1 /**************************************************************************
3 * Copyright © 2007 Red Hat Inc.
4 * Copyright © 2007-2012 Intel Corporation
5 * Copyright 2006 Tungsten Graphics, Inc., Bismarck, ND., USA
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the
10 * "Software"), to deal in the Software without restriction, including
11 * without limitation the rights to use, copy, modify, merge, publish,
12 * distribute, sub license, and/or sell copies of the Software, and to
13 * permit persons to whom the Software is furnished to do so, subject to
14 * the following conditions:
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
22 * USE OR OTHER DEALINGS IN THE SOFTWARE.
24 * The above copyright notice and this permission notice (including the
25 * next paragraph) shall be included in all copies or substantial portions
29 **************************************************************************/
31 * Authors: Thomas Hellström <thomas-at-tungstengraphics-dot-com>
32 * Keith Whitwell <keithw-at-tungstengraphics-dot-com>
33 * Eric Anholt <eric@anholt.net>
34 * Dave Airlie <airlied@linux.ie>
42 #include <util/u_atomic.h>
50 #include <sys/ioctl.h>
52 #include <sys/types.h>
57 #define ETIME ETIMEDOUT
59 #include "common/gen_debug.h"
60 #include "common/gen_device_info.h"
61 #include "libdrm_macros.h"
62 #include "main/macros.h"
63 #include "util/macros.h"
64 #include "util/hash_table.h"
65 #include "util/list.h"
66 #include "brw_bufmgr.h"
67 #include "brw_context.h"
80 #define memclear(s) memset(&s, 0, sizeof(s))
82 #define FILE_DEBUG_FLAG DEBUG_BUFMGR
85 atomic_add_unless(int *v
, int add
, int unless
)
89 while (c
!= unless
&& (old
= p_atomic_cmpxchg(v
, c
, c
+ add
)) != c
)
94 struct bo_cache_bucket
{
95 struct list_head head
;
102 pthread_mutex_t lock
;
104 /** Array of lists of cached gem objects of power-of-two sizes */
105 struct bo_cache_bucket cache_bucket
[14 * 4];
109 struct hash_table
*name_table
;
110 struct hash_table
*handle_table
;
116 static int bo_set_tiling_internal(struct brw_bo
*bo
, uint32_t tiling_mode
,
119 static void bo_free(struct brw_bo
*bo
);
122 key_hash_uint(const void *key
)
124 return _mesa_hash_data(key
, 4);
128 key_uint_equal(const void *a
, const void *b
)
130 return *((unsigned *) a
) == *((unsigned *) b
);
133 static struct brw_bo
*
134 hash_find_bo(struct hash_table
*ht
, unsigned int key
)
136 struct hash_entry
*entry
= _mesa_hash_table_search(ht
, &key
);
137 return entry
? (struct brw_bo
*) entry
->data
: NULL
;
141 bo_tile_size(struct brw_bufmgr
*bufmgr
, uint64_t size
, uint32_t tiling
)
143 if (tiling
== I915_TILING_NONE
)
146 /* 965+ just need multiples of page size for tiling */
147 return ALIGN(size
, 4096);
151 * Round a given pitch up to the minimum required for X tiling on a
152 * given chip. We use 512 as the minimum to allow for a later tiling
156 bo_tile_pitch(struct brw_bufmgr
*bufmgr
, uint32_t pitch
, uint32_t tiling
)
158 unsigned long tile_width
;
160 /* If untiled, then just align it so that we can do rendering
161 * to it with the 3D engine.
163 if (tiling
== I915_TILING_NONE
)
164 return ALIGN(pitch
, 64);
166 if (tiling
== I915_TILING_X
)
171 /* 965 is flexible */
172 return ALIGN(pitch
, tile_width
);
175 static struct bo_cache_bucket
*
176 bucket_for_size(struct brw_bufmgr
*bufmgr
, uint64_t size
)
180 for (i
= 0; i
< bufmgr
->num_buckets
; i
++) {
181 struct bo_cache_bucket
*bucket
= &bufmgr
->cache_bucket
[i
];
182 if (bucket
->size
>= size
) {
191 brw_bo_reference(struct brw_bo
*bo
)
193 p_atomic_inc(&bo
->refcount
);
197 brw_bo_busy(struct brw_bo
*bo
)
199 struct brw_bufmgr
*bufmgr
= bo
->bufmgr
;
200 struct drm_i915_gem_busy busy
;
204 busy
.handle
= bo
->gem_handle
;
206 ret
= drmIoctl(bufmgr
->fd
, DRM_IOCTL_I915_GEM_BUSY
, &busy
);
208 bo
->idle
= !busy
.busy
;
215 brw_bo_madvise(struct brw_bo
*bo
, int state
)
217 struct drm_i915_gem_madvise madv
;
220 madv
.handle
= bo
->gem_handle
;
223 drmIoctl(bo
->bufmgr
->fd
, DRM_IOCTL_I915_GEM_MADVISE
, &madv
);
225 return madv
.retained
;
228 /* drop the oldest entries that have been purged by the kernel */
230 brw_bo_cache_purge_bucket(struct brw_bufmgr
*bufmgr
,
231 struct bo_cache_bucket
*bucket
)
233 list_for_each_entry_safe(struct brw_bo
, bo
, &bucket
->head
, head
) {
234 if (brw_bo_madvise(bo
, I915_MADV_DONTNEED
))
242 static struct brw_bo
*
243 bo_alloc_internal(struct brw_bufmgr
*bufmgr
,
247 uint32_t tiling_mode
,
248 uint32_t stride
, uint64_t alignment
)
251 unsigned int page_size
= getpagesize();
253 struct bo_cache_bucket
*bucket
;
254 bool alloc_from_cache
;
256 bool for_render
= false;
258 if (flags
& BO_ALLOC_FOR_RENDER
)
261 /* Round the allocated size up to a power of two number of pages. */
262 bucket
= bucket_for_size(bufmgr
, size
);
264 /* If we don't have caching at this size, don't actually round the
267 if (bucket
== NULL
) {
269 if (bo_size
< page_size
)
272 bo_size
= bucket
->size
;
275 pthread_mutex_lock(&bufmgr
->lock
);
276 /* Get a buffer out of the cache if available */
278 alloc_from_cache
= false;
279 if (bucket
!= NULL
&& !list_empty(&bucket
->head
)) {
281 /* Allocate new render-target BOs from the tail (MRU)
282 * of the list, as it will likely be hot in the GPU
283 * cache and in the aperture for us.
285 bo
= LIST_ENTRY(struct brw_bo
, bucket
->head
.prev
, head
);
287 alloc_from_cache
= true;
288 bo
->align
= alignment
;
290 assert(alignment
== 0);
291 /* For non-render-target BOs (where we're probably
292 * going to map it first thing in order to fill it
293 * with data), check if the last BO in the cache is
294 * unbusy, and only reuse in that case. Otherwise,
295 * allocating a new buffer is probably faster than
296 * waiting for the GPU to finish.
298 bo
= LIST_ENTRY(struct brw_bo
, bucket
->head
.next
, head
);
299 if (!brw_bo_busy(bo
)) {
300 alloc_from_cache
= true;
305 if (alloc_from_cache
) {
306 if (!brw_bo_madvise(bo
, I915_MADV_WILLNEED
)) {
308 brw_bo_cache_purge_bucket(bufmgr
, bucket
);
312 if (bo_set_tiling_internal(bo
, tiling_mode
, stride
)) {
319 if (!alloc_from_cache
) {
320 struct drm_i915_gem_create create
;
322 bo
= calloc(1, sizeof(*bo
));
329 create
.size
= bo_size
;
331 ret
= drmIoctl(bufmgr
->fd
, DRM_IOCTL_I915_GEM_CREATE
, &create
);
337 bo
->gem_handle
= create
.handle
;
338 _mesa_hash_table_insert(bufmgr
->handle_table
, &bo
->gem_handle
, bo
);
341 bo
->align
= alignment
;
343 bo
->tiling_mode
= I915_TILING_NONE
;
344 bo
->swizzle_mode
= I915_BIT_6_SWIZZLE_NONE
;
347 if (bo_set_tiling_internal(bo
, tiling_mode
, stride
))
352 p_atomic_set(&bo
->refcount
, 1);
355 pthread_mutex_unlock(&bufmgr
->lock
);
357 DBG("bo_create: buf %d (%s) %ldb\n", bo
->gem_handle
, bo
->name
, size
);
364 pthread_mutex_unlock(&bufmgr
->lock
);
369 brw_bo_alloc(struct brw_bufmgr
*bufmgr
,
370 const char *name
, uint64_t size
, uint64_t alignment
)
372 return bo_alloc_internal(bufmgr
, name
, size
, 0, I915_TILING_NONE
, 0, 0);
376 brw_bo_alloc_tiled(struct brw_bufmgr
*bufmgr
, const char *name
,
377 int x
, int y
, int cpp
, uint32_t tiling
,
378 uint32_t *pitch
, unsigned flags
)
382 unsigned long aligned_y
, height_alignment
;
384 /* If we're tiled, our allocations are in 8 or 32-row blocks,
385 * so failure to align our height means that we won't allocate
388 * If we're untiled, we still have to align to 2 rows high
389 * because the data port accesses 2x2 blocks even if the
390 * bottom row isn't to be rendered, so failure to align means
391 * we could walk off the end of the GTT and fault. This is
392 * documented on 965, and may be the case on older chipsets
393 * too so we try to be careful.
396 height_alignment
= 2;
398 if (tiling
== I915_TILING_X
)
399 height_alignment
= 8;
400 else if (tiling
== I915_TILING_Y
)
401 height_alignment
= 32;
402 aligned_y
= ALIGN(y
, height_alignment
);
405 stride
= bo_tile_pitch(bufmgr
, stride
, tiling
);
406 size
= stride
* aligned_y
;
407 size
= bo_tile_size(bufmgr
, size
, tiling
);
410 if (tiling
== I915_TILING_NONE
)
413 return bo_alloc_internal(bufmgr
, name
, size
, flags
, tiling
, stride
, 0);
417 * Returns a brw_bo wrapping the given buffer object handle.
419 * This can be used when one application needs to pass a buffer object
423 brw_bo_gem_create_from_name(struct brw_bufmgr
*bufmgr
,
424 const char *name
, unsigned int handle
)
428 struct drm_gem_open open_arg
;
429 struct drm_i915_gem_get_tiling get_tiling
;
431 /* At the moment most applications only have a few named bo.
432 * For instance, in a DRI client only the render buffers passed
433 * between X and the client are named. And since X returns the
434 * alternating names for the front/back buffer a linear search
435 * provides a sufficiently fast match.
437 pthread_mutex_lock(&bufmgr
->lock
);
438 bo
= hash_find_bo(bufmgr
->name_table
, handle
);
440 brw_bo_reference(bo
);
445 open_arg
.name
= handle
;
446 ret
= drmIoctl(bufmgr
->fd
, DRM_IOCTL_GEM_OPEN
, &open_arg
);
448 DBG("Couldn't reference %s handle 0x%08x: %s\n",
449 name
, handle
, strerror(errno
));
453 /* Now see if someone has used a prime handle to get this
454 * object from the kernel before by looking through the list
455 * again for a matching gem_handle
457 bo
= hash_find_bo(bufmgr
->handle_table
, open_arg
.handle
);
459 brw_bo_reference(bo
);
463 bo
= calloc(1, sizeof(*bo
));
467 p_atomic_set(&bo
->refcount
, 1);
469 bo
->size
= open_arg
.size
;
473 bo
->gem_handle
= open_arg
.handle
;
475 bo
->global_name
= handle
;
476 bo
->reusable
= false;
478 _mesa_hash_table_insert(bufmgr
->handle_table
, &bo
->gem_handle
, bo
);
479 _mesa_hash_table_insert(bufmgr
->name_table
, &bo
->global_name
, bo
);
481 memclear(get_tiling
);
482 get_tiling
.handle
= bo
->gem_handle
;
483 ret
= drmIoctl(bufmgr
->fd
, DRM_IOCTL_I915_GEM_GET_TILING
, &get_tiling
);
487 bo
->tiling_mode
= get_tiling
.tiling_mode
;
488 bo
->swizzle_mode
= get_tiling
.swizzle_mode
;
489 /* XXX stride is unknown */
490 DBG("bo_create_from_handle: %d (%s)\n", handle
, bo
->name
);
493 pthread_mutex_unlock(&bufmgr
->lock
);
498 pthread_mutex_unlock(&bufmgr
->lock
);
503 bo_free(struct brw_bo
*bo
)
505 struct brw_bufmgr
*bufmgr
= bo
->bufmgr
;
506 struct drm_gem_close close
;
507 struct hash_entry
*entry
;
510 if (bo
->mem_virtual
) {
511 VG(VALGRIND_FREELIKE_BLOCK(bo
->mem_virtual
, 0));
512 drm_munmap(bo
->mem_virtual
, bo
->size
);
514 if (bo
->wc_virtual
) {
515 VG(VALGRIND_FREELIKE_BLOCK(bo
->wc_virtual
, 0));
516 drm_munmap(bo
->wc_virtual
, bo
->size
);
518 if (bo
->gtt_virtual
) {
519 drm_munmap(bo
->gtt_virtual
, bo
->size
);
522 if (bo
->global_name
) {
523 entry
= _mesa_hash_table_search(bufmgr
->name_table
, &bo
->global_name
);
524 _mesa_hash_table_remove(bufmgr
->name_table
, entry
);
526 entry
= _mesa_hash_table_search(bufmgr
->handle_table
, &bo
->gem_handle
);
527 _mesa_hash_table_remove(bufmgr
->handle_table
, entry
);
529 /* Close this object */
531 close
.handle
= bo
->gem_handle
;
532 ret
= drmIoctl(bufmgr
->fd
, DRM_IOCTL_GEM_CLOSE
, &close
);
534 DBG("DRM_IOCTL_GEM_CLOSE %d failed (%s): %s\n",
535 bo
->gem_handle
, bo
->name
, strerror(errno
));
541 bo_mark_mmaps_incoherent(struct brw_bo
*bo
)
545 VALGRIND_MAKE_MEM_NOACCESS(bo
->mem_virtual
, bo
->size
);
548 VALGRIND_MAKE_MEM_NOACCESS(bo
->wc_virtual
, bo
->size
);
551 VALGRIND_MAKE_MEM_NOACCESS(bo
->gtt_virtual
, bo
->size
);
555 /** Frees all cached buffers significantly older than @time. */
557 cleanup_bo_cache(struct brw_bufmgr
*bufmgr
, time_t time
)
561 if (bufmgr
->time
== time
)
564 for (i
= 0; i
< bufmgr
->num_buckets
; i
++) {
565 struct bo_cache_bucket
*bucket
= &bufmgr
->cache_bucket
[i
];
567 list_for_each_entry_safe(struct brw_bo
, bo
, &bucket
->head
, head
) {
568 if (time
- bo
->free_time
<= 1)
581 bo_unreference_final(struct brw_bo
*bo
, time_t time
)
583 struct brw_bufmgr
*bufmgr
= bo
->bufmgr
;
584 struct bo_cache_bucket
*bucket
;
586 DBG("bo_unreference final: %d (%s)\n", bo
->gem_handle
, bo
->name
);
588 /* Clear any left-over mappings */
590 DBG("bo freed with non-zero map-count %d\n", bo
->map_count
);
592 bo_mark_mmaps_incoherent(bo
);
595 bucket
= bucket_for_size(bufmgr
, bo
->size
);
596 /* Put the buffer into our internal cache for reuse if we can. */
597 if (bufmgr
->bo_reuse
&& bo
->reusable
&& bucket
!= NULL
&&
598 brw_bo_madvise(bo
, I915_MADV_DONTNEED
)) {
599 bo
->free_time
= time
;
604 list_addtail(&bo
->head
, &bucket
->head
);
611 brw_bo_unreference(struct brw_bo
*bo
)
616 assert(p_atomic_read(&bo
->refcount
) > 0);
618 if (atomic_add_unless(&bo
->refcount
, -1, 1)) {
619 struct brw_bufmgr
*bufmgr
= bo
->bufmgr
;
620 struct timespec time
;
622 clock_gettime(CLOCK_MONOTONIC
, &time
);
624 pthread_mutex_lock(&bufmgr
->lock
);
626 if (p_atomic_dec_zero(&bo
->refcount
)) {
627 bo_unreference_final(bo
, time
.tv_sec
);
628 cleanup_bo_cache(bufmgr
, time
.tv_sec
);
631 pthread_mutex_unlock(&bufmgr
->lock
);
636 set_domain(struct brw_context
*brw
, const char *action
,
637 struct brw_bo
*bo
, uint32_t read_domains
, uint32_t write_domain
)
639 struct drm_i915_gem_set_domain sd
= {
640 .handle
= bo
->gem_handle
,
641 .read_domains
= read_domains
,
642 .write_domain
= write_domain
,
645 double elapsed
= unlikely(brw
&& brw
->perf_debug
) ? -get_time() : 0.0;
647 if (drmIoctl(bo
->bufmgr
->fd
, DRM_IOCTL_I915_GEM_SET_DOMAIN
, &sd
) != 0) {
648 DBG("%s:%d: Error setting memory domains %d (%08x %08x): %s.\n",
649 __FILE__
, __LINE__
, bo
->gem_handle
, read_domains
, write_domain
,
653 if (unlikely(brw
&& brw
->perf_debug
)) {
654 elapsed
+= get_time();
655 if (elapsed
> 1e-5) /* 0.01ms */
656 perf_debug("%s a busy \"%s\" BO stalled and took %.03f ms.\n",
657 action
, bo
->name
, elapsed
* 1000);
662 brw_bo_map(struct brw_context
*brw
, struct brw_bo
*bo
, int write_enable
)
664 struct brw_bufmgr
*bufmgr
= bo
->bufmgr
;
667 pthread_mutex_lock(&bufmgr
->lock
);
669 if (!bo
->mem_virtual
) {
670 struct drm_i915_gem_mmap mmap_arg
;
672 DBG("bo_map: %d (%s), map_count=%d\n",
673 bo
->gem_handle
, bo
->name
, bo
->map_count
);
676 mmap_arg
.handle
= bo
->gem_handle
;
677 mmap_arg
.size
= bo
->size
;
678 ret
= drmIoctl(bufmgr
->fd
, DRM_IOCTL_I915_GEM_MMAP
, &mmap_arg
);
681 DBG("%s:%d: Error mapping buffer %d (%s): %s .\n",
682 __FILE__
, __LINE__
, bo
->gem_handle
, bo
->name
, strerror(errno
));
683 pthread_mutex_unlock(&bufmgr
->lock
);
687 VG(VALGRIND_MALLOCLIKE_BLOCK(mmap_arg
.addr_ptr
, mmap_arg
.size
, 0, 1));
688 bo
->mem_virtual
= (void *) (uintptr_t) mmap_arg
.addr_ptr
;
690 DBG("bo_map: %d (%s) -> %p\n", bo
->gem_handle
, bo
->name
, bo
->mem_virtual
);
691 bo
->virtual = bo
->mem_virtual
;
693 set_domain(brw
, "CPU mapping", bo
, I915_GEM_DOMAIN_CPU
,
694 write_enable
? I915_GEM_DOMAIN_CPU
: 0);
696 bo_mark_mmaps_incoherent(bo
);
697 VG(VALGRIND_MAKE_MEM_DEFINED(bo
->mem_virtual
, bo
->size
));
698 pthread_mutex_unlock(&bufmgr
->lock
);
704 map_gtt(struct brw_bo
*bo
)
706 struct brw_bufmgr
*bufmgr
= bo
->bufmgr
;
709 /* Get a mapping of the buffer if we haven't before. */
710 if (bo
->gtt_virtual
== NULL
) {
711 struct drm_i915_gem_mmap_gtt mmap_arg
;
713 DBG("bo_map_gtt: mmap %d (%s), map_count=%d\n",
714 bo
->gem_handle
, bo
->name
, bo
->map_count
);
717 mmap_arg
.handle
= bo
->gem_handle
;
719 /* Get the fake offset back... */
720 ret
= drmIoctl(bufmgr
->fd
, DRM_IOCTL_I915_GEM_MMAP_GTT
, &mmap_arg
);
723 DBG("%s:%d: Error preparing buffer map %d (%s): %s .\n",
724 __FILE__
, __LINE__
, bo
->gem_handle
, bo
->name
, strerror(errno
));
729 bo
->gtt_virtual
= drm_mmap(0, bo
->size
, PROT_READ
| PROT_WRITE
,
730 MAP_SHARED
, bufmgr
->fd
, mmap_arg
.offset
);
731 if (bo
->gtt_virtual
== MAP_FAILED
) {
732 bo
->gtt_virtual
= NULL
;
734 DBG("%s:%d: Error mapping buffer %d (%s): %s .\n",
735 __FILE__
, __LINE__
, bo
->gem_handle
, bo
->name
, strerror(errno
));
741 bo
->virtual = bo
->gtt_virtual
;
743 DBG("bo_map_gtt: %d (%s) -> %p\n", bo
->gem_handle
, bo
->name
,
750 brw_bo_map_gtt(struct brw_context
*brw
, struct brw_bo
*bo
)
752 struct brw_bufmgr
*bufmgr
= bo
->bufmgr
;
755 pthread_mutex_lock(&bufmgr
->lock
);
759 pthread_mutex_unlock(&bufmgr
->lock
);
763 /* Now move it to the GTT domain so that the GPU and CPU
764 * caches are flushed and the GPU isn't actively using the
767 * The pagefault handler does this domain change for us when
768 * it has unbound the BO from the GTT, but it's up to us to
769 * tell it when we're about to use things if we had done
770 * rendering and it still happens to be bound to the GTT.
772 set_domain(brw
, "GTT mapping", bo
,
773 I915_GEM_DOMAIN_GTT
, I915_GEM_DOMAIN_GTT
);
775 bo_mark_mmaps_incoherent(bo
);
776 VG(VALGRIND_MAKE_MEM_DEFINED(bo
->gtt_virtual
, bo
->size
));
777 pthread_mutex_unlock(&bufmgr
->lock
);
783 * Performs a mapping of the buffer object like the normal GTT
784 * mapping, but avoids waiting for the GPU to be done reading from or
785 * rendering to the buffer.
787 * This is used in the implementation of GL_ARB_map_buffer_range: The
788 * user asks to create a buffer, then does a mapping, fills some
789 * space, runs a drawing command, then asks to map it again without
790 * synchronizing because it guarantees that it won't write over the
791 * data that the GPU is busy using (or, more specifically, that if it
792 * does write over the data, it acknowledges that rendering is
797 brw_bo_map_unsynchronized(struct brw_context
*brw
, struct brw_bo
*bo
)
799 struct brw_bufmgr
*bufmgr
= bo
->bufmgr
;
802 /* If the CPU cache isn't coherent with the GTT, then use a
803 * regular synchronized mapping. The problem is that we don't
804 * track where the buffer was last used on the CPU side in
805 * terms of brw_bo_map vs brw_bo_map_gtt, so
806 * we would potentially corrupt the buffer even when the user
807 * does reasonable things.
809 if (!bufmgr
->has_llc
)
810 return brw_bo_map_gtt(brw
, bo
);
812 pthread_mutex_lock(&bufmgr
->lock
);
816 bo_mark_mmaps_incoherent(bo
);
817 VG(VALGRIND_MAKE_MEM_DEFINED(bo
->gtt_virtual
, bo
->size
));
820 pthread_mutex_unlock(&bufmgr
->lock
);
826 brw_bo_unmap(struct brw_bo
*bo
)
828 struct brw_bufmgr
*bufmgr
= bo
->bufmgr
;
831 pthread_mutex_lock(&bufmgr
->lock
);
833 if (bo
->map_count
<= 0) {
834 DBG("attempted to unmap an unmapped bo\n");
835 pthread_mutex_unlock(&bufmgr
->lock
);
836 /* Preserve the old behaviour of just treating this as a
837 * no-op rather than reporting the error.
842 if (--bo
->map_count
== 0) {
843 bo_mark_mmaps_incoherent(bo
);
846 pthread_mutex_unlock(&bufmgr
->lock
);
852 brw_bo_subdata(struct brw_bo
*bo
, uint64_t offset
,
853 uint64_t size
, const void *data
)
855 struct brw_bufmgr
*bufmgr
= bo
->bufmgr
;
856 struct drm_i915_gem_pwrite pwrite
;
860 pwrite
.handle
= bo
->gem_handle
;
861 pwrite
.offset
= offset
;
863 pwrite
.data_ptr
= (uint64_t) (uintptr_t) data
;
864 ret
= drmIoctl(bufmgr
->fd
, DRM_IOCTL_I915_GEM_PWRITE
, &pwrite
);
867 DBG("%s:%d: Error writing data to buffer %d: "
868 "(%"PRIu64
" %"PRIu64
") %s .\n",
869 __FILE__
, __LINE__
, bo
->gem_handle
, offset
, size
, strerror(errno
));
876 brw_bo_get_subdata(struct brw_bo
*bo
, uint64_t offset
,
877 uint64_t size
, void *data
)
879 struct brw_bufmgr
*bufmgr
= bo
->bufmgr
;
880 struct drm_i915_gem_pread pread
;
884 pread
.handle
= bo
->gem_handle
;
885 pread
.offset
= offset
;
887 pread
.data_ptr
= (uint64_t) (uintptr_t) data
;
888 ret
= drmIoctl(bufmgr
->fd
, DRM_IOCTL_I915_GEM_PREAD
, &pread
);
891 DBG("%s:%d: Error reading data from buffer %d: "
892 "(%"PRIu64
" %"PRIu64
") %s .\n",
893 __FILE__
, __LINE__
, bo
->gem_handle
, offset
, size
, strerror(errno
));
899 /** Waits for all GPU rendering with the object to have completed. */
901 brw_bo_wait_rendering(struct brw_context
*brw
, struct brw_bo
*bo
)
903 set_domain(brw
, "waiting for",
904 bo
, I915_GEM_DOMAIN_GTT
, I915_GEM_DOMAIN_GTT
);
908 * Waits on a BO for the given amount of time.
910 * @bo: buffer object to wait for
911 * @timeout_ns: amount of time to wait in nanoseconds.
912 * If value is less than 0, an infinite wait will occur.
914 * Returns 0 if the wait was successful ie. the last batch referencing the
915 * object has completed within the allotted time. Otherwise some negative return
916 * value describes the error. Of particular interest is -ETIME when the wait has
917 * failed to yield the desired result.
919 * Similar to brw_bo_wait_rendering except a timeout parameter allows
920 * the operation to give up after a certain amount of time. Another subtle
921 * difference is the internal locking semantics are different (this variant does
922 * not hold the lock for the duration of the wait). This makes the wait subject
923 * to a larger userspace race window.
925 * The implementation shall wait until the object is no longer actively
926 * referenced within a batch buffer at the time of the call. The wait will
927 * not guarantee that the buffer is re-issued via another thread, or an flinked
928 * handle. Userspace must make sure this race does not occur if such precision
931 * Note that some kernels have broken the inifite wait for negative values
932 * promise, upgrade to latest stable kernels if this is the case.
935 brw_bo_wait(struct brw_bo
*bo
, int64_t timeout_ns
)
937 struct brw_bufmgr
*bufmgr
= bo
->bufmgr
;
938 struct drm_i915_gem_wait wait
;
942 wait
.bo_handle
= bo
->gem_handle
;
943 wait
.timeout_ns
= timeout_ns
;
944 ret
= drmIoctl(bufmgr
->fd
, DRM_IOCTL_I915_GEM_WAIT
, &wait
);
952 brw_bufmgr_destroy(struct brw_bufmgr
*bufmgr
)
954 pthread_mutex_destroy(&bufmgr
->lock
);
956 /* Free any cached buffer objects we were going to reuse */
957 for (int i
= 0; i
< bufmgr
->num_buckets
; i
++) {
958 struct bo_cache_bucket
*bucket
= &bufmgr
->cache_bucket
[i
];
960 list_for_each_entry_safe(struct brw_bo
, bo
, &bucket
->head
, head
) {
967 _mesa_hash_table_destroy(bufmgr
->name_table
, NULL
);
968 _mesa_hash_table_destroy(bufmgr
->handle_table
, NULL
);
974 bo_set_tiling_internal(struct brw_bo
*bo
, uint32_t tiling_mode
,
977 struct brw_bufmgr
*bufmgr
= bo
->bufmgr
;
978 struct drm_i915_gem_set_tiling set_tiling
;
981 if (bo
->global_name
== 0 &&
982 tiling_mode
== bo
->tiling_mode
&& stride
== bo
->stride
)
985 memset(&set_tiling
, 0, sizeof(set_tiling
));
987 /* set_tiling is slightly broken and overwrites the
988 * input on the error path, so we have to open code
991 set_tiling
.handle
= bo
->gem_handle
;
992 set_tiling
.tiling_mode
= tiling_mode
;
993 set_tiling
.stride
= stride
;
995 ret
= ioctl(bufmgr
->fd
, DRM_IOCTL_I915_GEM_SET_TILING
, &set_tiling
);
996 } while (ret
== -1 && (errno
== EINTR
|| errno
== EAGAIN
));
1000 bo
->tiling_mode
= set_tiling
.tiling_mode
;
1001 bo
->swizzle_mode
= set_tiling
.swizzle_mode
;
1002 bo
->stride
= set_tiling
.stride
;
1007 brw_bo_get_tiling(struct brw_bo
*bo
, uint32_t *tiling_mode
,
1008 uint32_t *swizzle_mode
)
1010 *tiling_mode
= bo
->tiling_mode
;
1011 *swizzle_mode
= bo
->swizzle_mode
;
1016 brw_bo_gem_create_from_prime(struct brw_bufmgr
*bufmgr
, int prime_fd
,
1022 struct drm_i915_gem_get_tiling get_tiling
;
1024 pthread_mutex_lock(&bufmgr
->lock
);
1025 ret
= drmPrimeFDToHandle(bufmgr
->fd
, prime_fd
, &handle
);
1027 DBG("create_from_prime: failed to obtain handle from fd: %s\n",
1029 pthread_mutex_unlock(&bufmgr
->lock
);
1034 * See if the kernel has already returned this buffer to us. Just as
1035 * for named buffers, we must not create two bo's pointing at the same
1038 bo
= hash_find_bo(bufmgr
->handle_table
, handle
);
1040 brw_bo_reference(bo
);
1044 bo
= calloc(1, sizeof(*bo
));
1048 p_atomic_set(&bo
->refcount
, 1);
1050 /* Determine size of bo. The fd-to-handle ioctl really should
1051 * return the size, but it doesn't. If we have kernel 3.12 or
1052 * later, we can lseek on the prime fd to get the size. Older
1053 * kernels will just fail, in which case we fall back to the
1054 * provided (estimated or guess size). */
1055 ret
= lseek(prime_fd
, 0, SEEK_END
);
1061 bo
->bufmgr
= bufmgr
;
1063 bo
->gem_handle
= handle
;
1064 _mesa_hash_table_insert(bufmgr
->handle_table
, &bo
->gem_handle
, bo
);
1067 bo
->reusable
= false;
1069 memclear(get_tiling
);
1070 get_tiling
.handle
= bo
->gem_handle
;
1071 if (drmIoctl(bufmgr
->fd
, DRM_IOCTL_I915_GEM_GET_TILING
, &get_tiling
))
1074 bo
->tiling_mode
= get_tiling
.tiling_mode
;
1075 bo
->swizzle_mode
= get_tiling
.swizzle_mode
;
1076 /* XXX stride is unknown */
1079 pthread_mutex_unlock(&bufmgr
->lock
);
1084 pthread_mutex_unlock(&bufmgr
->lock
);
1089 brw_bo_gem_export_to_prime(struct brw_bo
*bo
, int *prime_fd
)
1091 struct brw_bufmgr
*bufmgr
= bo
->bufmgr
;
1093 if (drmPrimeHandleToFD(bufmgr
->fd
, bo
->gem_handle
,
1094 DRM_CLOEXEC
, prime_fd
) != 0)
1097 bo
->reusable
= false;
1103 brw_bo_flink(struct brw_bo
*bo
, uint32_t *name
)
1105 struct brw_bufmgr
*bufmgr
= bo
->bufmgr
;
1107 if (!bo
->global_name
) {
1108 struct drm_gem_flink flink
;
1111 flink
.handle
= bo
->gem_handle
;
1112 if (drmIoctl(bufmgr
->fd
, DRM_IOCTL_GEM_FLINK
, &flink
))
1115 pthread_mutex_lock(&bufmgr
->lock
);
1116 if (!bo
->global_name
) {
1117 bo
->global_name
= flink
.name
;
1118 bo
->reusable
= false;
1120 _mesa_hash_table_insert(bufmgr
->name_table
, &bo
->global_name
, bo
);
1122 pthread_mutex_unlock(&bufmgr
->lock
);
1125 *name
= bo
->global_name
;
1130 * Enables unlimited caching of buffer objects for reuse.
1132 * This is potentially very memory expensive, as the cache at each bucket
1133 * size is only bounded by how many buffers of that size we've managed to have
1134 * in flight at once.
1137 brw_bufmgr_enable_reuse(struct brw_bufmgr
*bufmgr
)
1139 bufmgr
->bo_reuse
= true;
1143 add_bucket(struct brw_bufmgr
*bufmgr
, int size
)
1145 unsigned int i
= bufmgr
->num_buckets
;
1147 assert(i
< ARRAY_SIZE(bufmgr
->cache_bucket
));
1149 list_inithead(&bufmgr
->cache_bucket
[i
].head
);
1150 bufmgr
->cache_bucket
[i
].size
= size
;
1151 bufmgr
->num_buckets
++;
1155 init_cache_buckets(struct brw_bufmgr
*bufmgr
)
1157 uint64_t size
, cache_max_size
= 64 * 1024 * 1024;
1159 /* OK, so power of two buckets was too wasteful of memory.
1160 * Give 3 other sizes between each power of two, to hopefully
1161 * cover things accurately enough. (The alternative is
1162 * probably to just go for exact matching of sizes, and assume
1163 * that for things like composited window resize the tiled
1164 * width/height alignment and rounding of sizes to pages will
1165 * get us useful cache hit rates anyway)
1167 add_bucket(bufmgr
, 4096);
1168 add_bucket(bufmgr
, 4096 * 2);
1169 add_bucket(bufmgr
, 4096 * 3);
1171 /* Initialize the linked lists for BO reuse cache. */
1172 for (size
= 4 * 4096; size
<= cache_max_size
; size
*= 2) {
1173 add_bucket(bufmgr
, size
);
1175 add_bucket(bufmgr
, size
+ size
* 1 / 4);
1176 add_bucket(bufmgr
, size
+ size
* 2 / 4);
1177 add_bucket(bufmgr
, size
+ size
* 3 / 4);
1182 brw_create_hw_context(struct brw_bufmgr
*bufmgr
)
1184 struct drm_i915_gem_context_create create
;
1188 ret
= drmIoctl(bufmgr
->fd
, DRM_IOCTL_I915_GEM_CONTEXT_CREATE
, &create
);
1190 DBG("DRM_IOCTL_I915_GEM_CONTEXT_CREATE failed: %s\n", strerror(errno
));
1194 return create
.ctx_id
;
1198 brw_destroy_hw_context(struct brw_bufmgr
*bufmgr
, uint32_t ctx_id
)
1200 struct drm_i915_gem_context_destroy d
= {.ctx_id
= ctx_id
};
1203 drmIoctl(bufmgr
->fd
, DRM_IOCTL_I915_GEM_CONTEXT_DESTROY
, &d
) != 0) {
1204 fprintf(stderr
, "DRM_IOCTL_I915_GEM_CONTEXT_DESTROY failed: %s\n",
1210 brw_reg_read(struct brw_bufmgr
*bufmgr
, uint32_t offset
, uint64_t *result
)
1212 struct drm_i915_reg_read reg_read
;
1216 reg_read
.offset
= offset
;
1218 ret
= drmIoctl(bufmgr
->fd
, DRM_IOCTL_I915_REG_READ
, ®_read
);
1220 *result
= reg_read
.val
;
1225 brw_bo_map__gtt(struct brw_bo
*bo
)
1227 struct brw_bufmgr
*bufmgr
= bo
->bufmgr
;
1229 if (bo
->gtt_virtual
)
1230 return bo
->gtt_virtual
;
1232 pthread_mutex_lock(&bufmgr
->lock
);
1233 if (bo
->gtt_virtual
== NULL
) {
1234 struct drm_i915_gem_mmap_gtt mmap_arg
;
1237 DBG("bo_map_gtt: mmap %d (%s), map_count=%d\n",
1238 bo
->gem_handle
, bo
->name
, bo
->map_count
);
1241 mmap_arg
.handle
= bo
->gem_handle
;
1243 /* Get the fake offset back... */
1245 if (drmIoctl(bufmgr
->fd
, DRM_IOCTL_I915_GEM_MMAP_GTT
, &mmap_arg
) == 0) {
1247 ptr
= drm_mmap(0, bo
->size
, PROT_READ
| PROT_WRITE
,
1248 MAP_SHARED
, bufmgr
->fd
, mmap_arg
.offset
);
1250 if (ptr
== MAP_FAILED
) {
1255 bo
->gtt_virtual
= ptr
;
1257 pthread_mutex_unlock(&bufmgr
->lock
);
1259 return bo
->gtt_virtual
;
1263 brw_bo_map__cpu(struct brw_bo
*bo
)
1265 struct brw_bufmgr
*bufmgr
= bo
->bufmgr
;
1267 if (bo
->mem_virtual
)
1268 return bo
->mem_virtual
;
1270 pthread_mutex_lock(&bufmgr
->lock
);
1271 if (!bo
->mem_virtual
) {
1272 struct drm_i915_gem_mmap mmap_arg
;
1274 DBG("bo_map: %d (%s), map_count=%d\n",
1275 bo
->gem_handle
, bo
->name
, bo
->map_count
);
1278 mmap_arg
.handle
= bo
->gem_handle
;
1279 mmap_arg
.size
= bo
->size
;
1280 if (drmIoctl(bufmgr
->fd
, DRM_IOCTL_I915_GEM_MMAP
, &mmap_arg
)) {
1281 DBG("%s:%d: Error mapping buffer %d (%s): %s .\n",
1282 __FILE__
, __LINE__
, bo
->gem_handle
, bo
->name
, strerror(errno
));
1285 VG(VALGRIND_MALLOCLIKE_BLOCK
1286 (mmap_arg
.addr_ptr
, mmap_arg
.size
, 0, 1));
1287 bo
->mem_virtual
= (void *) (uintptr_t) mmap_arg
.addr_ptr
;
1290 pthread_mutex_unlock(&bufmgr
->lock
);
1292 return bo
->mem_virtual
;
1296 brw_bo_map__wc(struct brw_bo
*bo
)
1298 struct brw_bufmgr
*bufmgr
= bo
->bufmgr
;
1301 return bo
->wc_virtual
;
1303 pthread_mutex_lock(&bufmgr
->lock
);
1304 if (!bo
->wc_virtual
) {
1305 struct drm_i915_gem_mmap mmap_arg
;
1307 DBG("bo_map: %d (%s), map_count=%d\n",
1308 bo
->gem_handle
, bo
->name
, bo
->map_count
);
1311 mmap_arg
.handle
= bo
->gem_handle
;
1312 mmap_arg
.size
= bo
->size
;
1313 mmap_arg
.flags
= I915_MMAP_WC
;
1314 if (drmIoctl(bufmgr
->fd
, DRM_IOCTL_I915_GEM_MMAP
, &mmap_arg
)) {
1315 DBG("%s:%d: Error mapping buffer %d (%s): %s .\n",
1316 __FILE__
, __LINE__
, bo
->gem_handle
, bo
->name
, strerror(errno
));
1319 VG(VALGRIND_MALLOCLIKE_BLOCK
1320 (mmap_arg
.addr_ptr
, mmap_arg
.size
, 0, 1));
1321 bo
->wc_virtual
= (void *) (uintptr_t) mmap_arg
.addr_ptr
;
1324 pthread_mutex_unlock(&bufmgr
->lock
);
1326 return bo
->wc_virtual
;
1330 * Initializes the GEM buffer manager, which uses the kernel to allocate, map,
1331 * and manage map buffer objections.
1333 * \param fd File descriptor of the opened DRM device.
1336 brw_bufmgr_init(struct gen_device_info
*devinfo
, int fd
, int batch_size
)
1338 struct brw_bufmgr
*bufmgr
;
1340 bufmgr
= calloc(1, sizeof(*bufmgr
));
1344 /* Handles to buffer objects belong to the device fd and are not
1345 * reference counted by the kernel. If the same fd is used by
1346 * multiple parties (threads sharing the same screen bufmgr, or
1347 * even worse the same device fd passed to multiple libraries)
1348 * ownership of those handles is shared by those independent parties.
1350 * Don't do this! Ensure that each library/bufmgr has its own device
1351 * fd so that its namespace does not clash with another.
1355 if (pthread_mutex_init(&bufmgr
->lock
, NULL
) != 0) {
1360 bufmgr
->has_llc
= devinfo
->has_llc
;
1362 init_cache_buckets(bufmgr
);
1364 bufmgr
->name_table
=
1365 _mesa_hash_table_create(NULL
, key_hash_uint
, key_uint_equal
);
1366 bufmgr
->handle_table
=
1367 _mesa_hash_table_create(NULL
, key_hash_uint
, key_uint_equal
);