2 * Copyright © 2007 Red Hat Inc.
3 * Copyright © 2007-2017 Intel Corporation
4 * Copyright © 2006 VMware, Inc.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice (including the next
15 * paragraph) shall be included in all copies or substantial portions of the
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
28 * Authors: Thomas Hellström <thellstrom@vmware.com>
29 * Keith Whitwell <keithw@vmware.com>
30 * Eric Anholt <eric@anholt.net>
31 * Dave Airlie <airlied@linux.ie>
39 #include <util/u_atomic.h>
46 #include <sys/ioctl.h>
48 #include <sys/types.h>
53 #define ETIME ETIMEDOUT
55 #include "common/gen_clflush.h"
56 #include "common/gen_debug.h"
57 #include "common/gen_device_info.h"
58 #include "libdrm_macros.h"
59 #include "main/macros.h"
60 #include "util/macros.h"
61 #include "util/hash_table.h"
62 #include "util/list.h"
63 #include "brw_bufmgr.h"
64 #include "brw_context.h"
77 /* VALGRIND_FREELIKE_BLOCK unfortunately does not actually undo the earlier
78 * VALGRIND_MALLOCLIKE_BLOCK but instead leaves vg convinced the memory is
79 * leaked. All because it does not call VG(cli_free) from its
80 * VG_USERREQ__FREELIKE_BLOCK handler. Instead of treating the memory like
81 * and allocation, we mark it available for use upon mmapping and remove
84 #define VG_DEFINED(ptr, size) VG(VALGRIND_MAKE_MEM_DEFINED(ptr, size))
85 #define VG_NOACCESS(ptr, size) VG(VALGRIND_MAKE_MEM_NOACCESS(ptr, size))
87 #define memclear(s) memset(&s, 0, sizeof(s))
89 #define FILE_DEBUG_FLAG DEBUG_BUFMGR
92 atomic_add_unless(int *v
, int add
, int unless
)
96 while (c
!= unless
&& (old
= p_atomic_cmpxchg(v
, c
, c
+ add
)) != c
)
101 struct bo_cache_bucket
{
102 struct list_head head
;
111 /** Array of lists of cached gem objects of power-of-two sizes */
112 struct bo_cache_bucket cache_bucket
[14 * 4];
116 struct hash_table
*name_table
;
117 struct hash_table
*handle_table
;
124 static int bo_set_tiling_internal(struct brw_bo
*bo
, uint32_t tiling_mode
,
127 static void bo_free(struct brw_bo
*bo
);
130 key_hash_uint(const void *key
)
132 return _mesa_hash_data(key
, 4);
136 key_uint_equal(const void *a
, const void *b
)
138 return *((unsigned *) a
) == *((unsigned *) b
);
141 static struct brw_bo
*
142 hash_find_bo(struct hash_table
*ht
, unsigned int key
)
144 struct hash_entry
*entry
= _mesa_hash_table_search(ht
, &key
);
145 return entry
? (struct brw_bo
*) entry
->data
: NULL
;
149 bo_tile_size(struct brw_bufmgr
*bufmgr
, uint64_t size
, uint32_t tiling
)
151 if (tiling
== I915_TILING_NONE
)
154 /* 965+ just need multiples of page size for tiling */
155 return ALIGN(size
, 4096);
159 * Round a given pitch up to the minimum required for X tiling on a
160 * given chip. We use 512 as the minimum to allow for a later tiling
164 bo_tile_pitch(struct brw_bufmgr
*bufmgr
, uint32_t pitch
, uint32_t tiling
)
166 unsigned long tile_width
;
168 /* If untiled, then just align it so that we can do rendering
169 * to it with the 3D engine.
171 if (tiling
== I915_TILING_NONE
)
172 return ALIGN(pitch
, 64);
174 if (tiling
== I915_TILING_X
)
179 /* 965 is flexible */
180 return ALIGN(pitch
, tile_width
);
183 static struct bo_cache_bucket
*
184 bucket_for_size(struct brw_bufmgr
*bufmgr
, uint64_t size
)
188 for (i
= 0; i
< bufmgr
->num_buckets
; i
++) {
189 struct bo_cache_bucket
*bucket
= &bufmgr
->cache_bucket
[i
];
190 if (bucket
->size
>= size
) {
199 brw_bo_busy(struct brw_bo
*bo
)
201 struct brw_bufmgr
*bufmgr
= bo
->bufmgr
;
202 struct drm_i915_gem_busy busy
;
206 busy
.handle
= bo
->gem_handle
;
208 ret
= drmIoctl(bufmgr
->fd
, DRM_IOCTL_I915_GEM_BUSY
, &busy
);
210 bo
->idle
= !busy
.busy
;
217 brw_bo_madvise(struct brw_bo
*bo
, int state
)
219 struct drm_i915_gem_madvise madv
;
222 madv
.handle
= bo
->gem_handle
;
225 drmIoctl(bo
->bufmgr
->fd
, DRM_IOCTL_I915_GEM_MADVISE
, &madv
);
227 return madv
.retained
;
230 /* drop the oldest entries that have been purged by the kernel */
232 brw_bo_cache_purge_bucket(struct brw_bufmgr
*bufmgr
,
233 struct bo_cache_bucket
*bucket
)
235 list_for_each_entry_safe(struct brw_bo
, bo
, &bucket
->head
, head
) {
236 if (brw_bo_madvise(bo
, I915_MADV_DONTNEED
))
244 static struct brw_bo
*
245 bo_alloc_internal(struct brw_bufmgr
*bufmgr
,
249 uint32_t tiling_mode
,
250 uint32_t stride
, uint64_t alignment
)
253 unsigned int page_size
= getpagesize();
255 struct bo_cache_bucket
*bucket
;
256 bool alloc_from_cache
;
261 if (flags
& BO_ALLOC_BUSY
)
264 if (flags
& BO_ALLOC_ZEROED
)
267 /* BUSY does doesn't really jive with ZEROED as we have to wait for it to
268 * be idle before we can memset. Just disallow that combination.
270 assert(!(busy
&& zeroed
));
272 /* Round the allocated size up to a power of two number of pages. */
273 bucket
= bucket_for_size(bufmgr
, size
);
275 /* If we don't have caching at this size, don't actually round the
278 if (bucket
== NULL
) {
280 if (bo_size
< page_size
)
283 bo_size
= bucket
->size
;
286 mtx_lock(&bufmgr
->lock
);
287 /* Get a buffer out of the cache if available */
289 alloc_from_cache
= false;
290 if (bucket
!= NULL
&& !list_empty(&bucket
->head
)) {
291 if (busy
&& !zeroed
) {
292 /* Allocate new render-target BOs from the tail (MRU)
293 * of the list, as it will likely be hot in the GPU
294 * cache and in the aperture for us. If the caller
295 * asked us to zero the buffer, we don't want this
296 * because we are going to mmap it.
298 bo
= LIST_ENTRY(struct brw_bo
, bucket
->head
.prev
, head
);
300 alloc_from_cache
= true;
301 bo
->align
= alignment
;
303 assert(alignment
== 0);
304 /* For non-render-target BOs (where we're probably
305 * going to map it first thing in order to fill it
306 * with data), check if the last BO in the cache is
307 * unbusy, and only reuse in that case. Otherwise,
308 * allocating a new buffer is probably faster than
309 * waiting for the GPU to finish.
311 bo
= LIST_ENTRY(struct brw_bo
, bucket
->head
.next
, head
);
312 if (!brw_bo_busy(bo
)) {
313 alloc_from_cache
= true;
318 if (alloc_from_cache
) {
319 if (!brw_bo_madvise(bo
, I915_MADV_WILLNEED
)) {
321 brw_bo_cache_purge_bucket(bufmgr
, bucket
);
325 if (bo_set_tiling_internal(bo
, tiling_mode
, stride
)) {
331 void *map
= brw_bo_map(NULL
, bo
, MAP_WRITE
| MAP_RAW
);
336 memset(map
, 0, bo_size
);
341 if (!alloc_from_cache
) {
342 struct drm_i915_gem_create create
;
344 bo
= calloc(1, sizeof(*bo
));
352 create
.size
= bo_size
;
354 /* All new BOs we get from the kernel are zeroed, so we don't need to
355 * worry about that here.
357 ret
= drmIoctl(bufmgr
->fd
, DRM_IOCTL_I915_GEM_CREATE
, &create
);
363 bo
->gem_handle
= create
.handle
;
364 _mesa_hash_table_insert(bufmgr
->handle_table
, &bo
->gem_handle
, bo
);
367 bo
->align
= alignment
;
369 bo
->tiling_mode
= I915_TILING_NONE
;
370 bo
->swizzle_mode
= I915_BIT_6_SWIZZLE_NONE
;
373 if (bo_set_tiling_internal(bo
, tiling_mode
, stride
))
376 /* Calling set_domain() will allocate pages for the BO outside of the
377 * struct mutex lock in the kernel, which is more efficient than waiting
378 * to create them during the first execbuf that uses the BO.
380 struct drm_i915_gem_set_domain sd
= {
381 .handle
= bo
->gem_handle
,
382 .read_domains
= I915_GEM_DOMAIN_CPU
,
386 if (drmIoctl(bo
->bufmgr
->fd
, DRM_IOCTL_I915_GEM_SET_DOMAIN
, &sd
) != 0)
391 p_atomic_set(&bo
->refcount
, 1);
393 bo
->cache_coherent
= bufmgr
->has_llc
;
396 mtx_unlock(&bufmgr
->lock
);
398 DBG("bo_create: buf %d (%s) %llub\n", bo
->gem_handle
, bo
->name
,
399 (unsigned long long) size
);
406 mtx_unlock(&bufmgr
->lock
);
411 brw_bo_alloc(struct brw_bufmgr
*bufmgr
,
412 const char *name
, uint64_t size
, uint64_t alignment
)
414 return bo_alloc_internal(bufmgr
, name
, size
, 0, I915_TILING_NONE
, 0, 0);
418 brw_bo_alloc_tiled(struct brw_bufmgr
*bufmgr
, const char *name
,
419 uint64_t size
, uint32_t tiling_mode
, uint32_t pitch
,
422 return bo_alloc_internal(bufmgr
, name
, size
, flags
, tiling_mode
, pitch
, 0);
426 brw_bo_alloc_tiled_2d(struct brw_bufmgr
*bufmgr
, const char *name
,
427 int x
, int y
, int cpp
, uint32_t tiling
,
428 uint32_t *pitch
, unsigned flags
)
432 unsigned long aligned_y
, height_alignment
;
434 /* If we're tiled, our allocations are in 8 or 32-row blocks,
435 * so failure to align our height means that we won't allocate
438 * If we're untiled, we still have to align to 2 rows high
439 * because the data port accesses 2x2 blocks even if the
440 * bottom row isn't to be rendered, so failure to align means
441 * we could walk off the end of the GTT and fault. This is
442 * documented on 965, and may be the case on older chipsets
443 * too so we try to be careful.
446 height_alignment
= 2;
448 if (tiling
== I915_TILING_X
)
449 height_alignment
= 8;
450 else if (tiling
== I915_TILING_Y
)
451 height_alignment
= 32;
452 aligned_y
= ALIGN(y
, height_alignment
);
455 stride
= bo_tile_pitch(bufmgr
, stride
, tiling
);
456 size
= stride
* aligned_y
;
457 size
= bo_tile_size(bufmgr
, size
, tiling
);
460 if (tiling
== I915_TILING_NONE
)
463 return bo_alloc_internal(bufmgr
, name
, size
, flags
, tiling
, stride
, 0);
467 * Returns a brw_bo wrapping the given buffer object handle.
469 * This can be used when one application needs to pass a buffer object
473 brw_bo_gem_create_from_name(struct brw_bufmgr
*bufmgr
,
474 const char *name
, unsigned int handle
)
478 struct drm_gem_open open_arg
;
479 struct drm_i915_gem_get_tiling get_tiling
;
481 /* At the moment most applications only have a few named bo.
482 * For instance, in a DRI client only the render buffers passed
483 * between X and the client are named. And since X returns the
484 * alternating names for the front/back buffer a linear search
485 * provides a sufficiently fast match.
487 mtx_lock(&bufmgr
->lock
);
488 bo
= hash_find_bo(bufmgr
->name_table
, handle
);
490 brw_bo_reference(bo
);
495 open_arg
.name
= handle
;
496 ret
= drmIoctl(bufmgr
->fd
, DRM_IOCTL_GEM_OPEN
, &open_arg
);
498 DBG("Couldn't reference %s handle 0x%08x: %s\n",
499 name
, handle
, strerror(errno
));
503 /* Now see if someone has used a prime handle to get this
504 * object from the kernel before by looking through the list
505 * again for a matching gem_handle
507 bo
= hash_find_bo(bufmgr
->handle_table
, open_arg
.handle
);
509 brw_bo_reference(bo
);
513 bo
= calloc(1, sizeof(*bo
));
517 p_atomic_set(&bo
->refcount
, 1);
519 bo
->size
= open_arg
.size
;
522 bo
->gem_handle
= open_arg
.handle
;
524 bo
->global_name
= handle
;
525 bo
->reusable
= false;
528 _mesa_hash_table_insert(bufmgr
->handle_table
, &bo
->gem_handle
, bo
);
529 _mesa_hash_table_insert(bufmgr
->name_table
, &bo
->global_name
, bo
);
531 memclear(get_tiling
);
532 get_tiling
.handle
= bo
->gem_handle
;
533 ret
= drmIoctl(bufmgr
->fd
, DRM_IOCTL_I915_GEM_GET_TILING
, &get_tiling
);
537 bo
->tiling_mode
= get_tiling
.tiling_mode
;
538 bo
->swizzle_mode
= get_tiling
.swizzle_mode
;
539 /* XXX stride is unknown */
540 DBG("bo_create_from_handle: %d (%s)\n", handle
, bo
->name
);
543 mtx_unlock(&bufmgr
->lock
);
548 mtx_unlock(&bufmgr
->lock
);
553 bo_free(struct brw_bo
*bo
)
555 struct brw_bufmgr
*bufmgr
= bo
->bufmgr
;
556 struct drm_gem_close close
;
557 struct hash_entry
*entry
;
561 VG_NOACCESS(bo
->map_cpu
, bo
->size
);
562 drm_munmap(bo
->map_cpu
, bo
->size
);
565 VG_NOACCESS(bo
->map_wc
, bo
->size
);
566 drm_munmap(bo
->map_wc
, bo
->size
);
569 VG_NOACCESS(bo
->map_gtt
, bo
->size
);
570 drm_munmap(bo
->map_gtt
, bo
->size
);
573 if (bo
->global_name
) {
574 entry
= _mesa_hash_table_search(bufmgr
->name_table
, &bo
->global_name
);
575 _mesa_hash_table_remove(bufmgr
->name_table
, entry
);
577 entry
= _mesa_hash_table_search(bufmgr
->handle_table
, &bo
->gem_handle
);
578 _mesa_hash_table_remove(bufmgr
->handle_table
, entry
);
580 /* Close this object */
582 close
.handle
= bo
->gem_handle
;
583 ret
= drmIoctl(bufmgr
->fd
, DRM_IOCTL_GEM_CLOSE
, &close
);
585 DBG("DRM_IOCTL_GEM_CLOSE %d failed (%s): %s\n",
586 bo
->gem_handle
, bo
->name
, strerror(errno
));
591 /** Frees all cached buffers significantly older than @time. */
593 cleanup_bo_cache(struct brw_bufmgr
*bufmgr
, time_t time
)
597 if (bufmgr
->time
== time
)
600 for (i
= 0; i
< bufmgr
->num_buckets
; i
++) {
601 struct bo_cache_bucket
*bucket
= &bufmgr
->cache_bucket
[i
];
603 list_for_each_entry_safe(struct brw_bo
, bo
, &bucket
->head
, head
) {
604 if (time
- bo
->free_time
<= 1)
617 bo_unreference_final(struct brw_bo
*bo
, time_t time
)
619 struct brw_bufmgr
*bufmgr
= bo
->bufmgr
;
620 struct bo_cache_bucket
*bucket
;
622 DBG("bo_unreference final: %d (%s)\n", bo
->gem_handle
, bo
->name
);
624 bucket
= bucket_for_size(bufmgr
, bo
->size
);
625 /* Put the buffer into our internal cache for reuse if we can. */
626 if (bufmgr
->bo_reuse
&& bo
->reusable
&& bucket
!= NULL
&&
627 brw_bo_madvise(bo
, I915_MADV_DONTNEED
)) {
628 bo
->free_time
= time
;
633 list_addtail(&bo
->head
, &bucket
->head
);
640 brw_bo_unreference(struct brw_bo
*bo
)
645 assert(p_atomic_read(&bo
->refcount
) > 0);
647 if (atomic_add_unless(&bo
->refcount
, -1, 1)) {
648 struct brw_bufmgr
*bufmgr
= bo
->bufmgr
;
649 struct timespec time
;
651 clock_gettime(CLOCK_MONOTONIC
, &time
);
653 mtx_lock(&bufmgr
->lock
);
655 if (p_atomic_dec_zero(&bo
->refcount
)) {
656 bo_unreference_final(bo
, time
.tv_sec
);
657 cleanup_bo_cache(bufmgr
, time
.tv_sec
);
660 mtx_unlock(&bufmgr
->lock
);
665 bo_wait_with_stall_warning(struct brw_context
*brw
,
669 double elapsed
= unlikely(brw
&& brw
->perf_debug
) ? -get_time() : 0.0;
671 brw_bo_wait_rendering(bo
);
673 if (unlikely(brw
&& brw
->perf_debug
)) {
674 elapsed
+= get_time();
675 if (elapsed
> 1e-5) /* 0.01ms */
676 perf_debug("%s a busy \"%s\" BO stalled and took %.03f ms.\n",
677 action
, bo
->name
, elapsed
* 1000);
682 print_flags(unsigned flags
)
684 if (flags
& MAP_READ
)
686 if (flags
& MAP_WRITE
)
688 if (flags
& MAP_ASYNC
)
690 if (flags
& MAP_PERSISTENT
)
692 if (flags
& MAP_COHERENT
)
700 brw_bo_map_cpu(struct brw_context
*brw
, struct brw_bo
*bo
, unsigned flags
)
702 struct brw_bufmgr
*bufmgr
= bo
->bufmgr
;
704 /* We disallow CPU maps for writing to non-coherent buffers, as the
705 * CPU map can become invalidated when a batch is flushed out, which
706 * can happen at unpredictable times. You should use WC maps instead.
708 assert(bo
->cache_coherent
|| !(flags
& MAP_WRITE
));
711 struct drm_i915_gem_mmap mmap_arg
;
714 DBG("brw_bo_map_cpu: %d (%s)\n", bo
->gem_handle
, bo
->name
);
717 mmap_arg
.handle
= bo
->gem_handle
;
718 mmap_arg
.size
= bo
->size
;
719 int ret
= drmIoctl(bufmgr
->fd
, DRM_IOCTL_I915_GEM_MMAP
, &mmap_arg
);
722 DBG("%s:%d: Error mapping buffer %d (%s): %s .\n",
723 __FILE__
, __LINE__
, bo
->gem_handle
, bo
->name
, strerror(errno
));
726 map
= (void *) (uintptr_t) mmap_arg
.addr_ptr
;
727 VG_DEFINED(map
, bo
->size
);
729 if (p_atomic_cmpxchg(&bo
->map_cpu
, NULL
, map
)) {
730 VG_NOACCESS(map
, bo
->size
);
731 drm_munmap(map
, bo
->size
);
736 DBG("brw_bo_map_cpu: %d (%s) -> %p, ", bo
->gem_handle
, bo
->name
,
740 if (!(flags
& MAP_ASYNC
)) {
741 bo_wait_with_stall_warning(brw
, bo
, "CPU mapping");
744 if (!bo
->cache_coherent
&& !bo
->bufmgr
->has_llc
) {
745 /* If we're reusing an existing CPU mapping, the CPU caches may
746 * contain stale data from the last time we read from that mapping.
747 * (With the BO cache, it might even be data from a previous buffer!)
748 * Even if it's a brand new mapping, the kernel may have zeroed the
749 * buffer via CPU writes.
751 * We need to invalidate those cachelines so that we see the latest
752 * contents, and so long as we only read from the CPU mmap we do not
753 * need to write those cachelines back afterwards.
755 * On LLC, the emprical evidence suggests that writes from the GPU
756 * that bypass the LLC (i.e. for scanout) do *invalidate* the CPU
757 * cachelines. (Other reads, such as the display engine, bypass the
758 * LLC entirely requiring us to keep dirty pixels for the scanout
761 gen_invalidate_range(bo
->map_cpu
, bo
->size
);
768 brw_bo_map_wc(struct brw_context
*brw
, struct brw_bo
*bo
, unsigned flags
)
770 struct brw_bufmgr
*bufmgr
= bo
->bufmgr
;
772 if (!bufmgr
->has_mmap_wc
)
776 struct drm_i915_gem_mmap mmap_arg
;
779 DBG("brw_bo_map_wc: %d (%s)\n", bo
->gem_handle
, bo
->name
);
782 mmap_arg
.handle
= bo
->gem_handle
;
783 mmap_arg
.size
= bo
->size
;
784 mmap_arg
.flags
= I915_MMAP_WC
;
785 int ret
= drmIoctl(bufmgr
->fd
, DRM_IOCTL_I915_GEM_MMAP
, &mmap_arg
);
788 DBG("%s:%d: Error mapping buffer %d (%s): %s .\n",
789 __FILE__
, __LINE__
, bo
->gem_handle
, bo
->name
, strerror(errno
));
793 map
= (void *) (uintptr_t) mmap_arg
.addr_ptr
;
794 VG_DEFINED(map
, bo
->size
);
796 if (p_atomic_cmpxchg(&bo
->map_wc
, NULL
, map
)) {
797 VG_NOACCESS(map
, bo
->size
);
798 drm_munmap(map
, bo
->size
);
803 DBG("brw_bo_map_wc: %d (%s) -> %p\n", bo
->gem_handle
, bo
->name
, bo
->map_wc
);
806 if (!(flags
& MAP_ASYNC
)) {
807 bo_wait_with_stall_warning(brw
, bo
, "WC mapping");
814 * Perform an uncached mapping via the GTT.
816 * Write access through the GTT is not quite fully coherent. On low power
817 * systems especially, like modern Atoms, we can observe reads from RAM before
818 * the write via GTT has landed. A write memory barrier that flushes the Write
819 * Combining Buffer (i.e. sfence/mfence) is not sufficient to order the later
820 * read after the write as the GTT write suffers a small delay through the GTT
821 * indirection. The kernel uses an uncached mmio read to ensure the GTT write
822 * is ordered with reads (either by the GPU, WB or WC) and unconditionally
823 * flushes prior to execbuf submission. However, if we are not informing the
824 * kernel about our GTT writes, it will not flush before earlier access, such
825 * as when using the cmdparser. Similarly, we need to be careful if we should
826 * ever issue a CPU read immediately following a GTT write.
828 * Telling the kernel about write access also has one more important
829 * side-effect. Upon receiving notification about the write, it cancels any
830 * scanout buffering for FBC/PSR and friends. Later FBC/PSR is then flushed by
831 * either SW_FINISH or DIRTYFB. The presumption is that we never write to the
832 * actual scanout via a mmaping, only to a backbuffer and so all the FBC/PSR
833 * tracking is handled on the buffer exchange instead.
836 brw_bo_map_gtt(struct brw_context
*brw
, struct brw_bo
*bo
, unsigned flags
)
838 struct brw_bufmgr
*bufmgr
= bo
->bufmgr
;
840 /* Get a mapping of the buffer if we haven't before. */
841 if (bo
->map_gtt
== NULL
) {
842 struct drm_i915_gem_mmap_gtt mmap_arg
;
845 DBG("bo_map_gtt: mmap %d (%s)\n", bo
->gem_handle
, bo
->name
);
848 mmap_arg
.handle
= bo
->gem_handle
;
850 /* Get the fake offset back... */
851 int ret
= drmIoctl(bufmgr
->fd
, DRM_IOCTL_I915_GEM_MMAP_GTT
, &mmap_arg
);
853 DBG("%s:%d: Error preparing buffer map %d (%s): %s .\n",
854 __FILE__
, __LINE__
, bo
->gem_handle
, bo
->name
, strerror(errno
));
859 map
= drm_mmap(0, bo
->size
, PROT_READ
| PROT_WRITE
,
860 MAP_SHARED
, bufmgr
->fd
, mmap_arg
.offset
);
861 if (map
== MAP_FAILED
) {
862 DBG("%s:%d: Error mapping buffer %d (%s): %s .\n",
863 __FILE__
, __LINE__
, bo
->gem_handle
, bo
->name
, strerror(errno
));
867 /* We don't need to use VALGRIND_MALLOCLIKE_BLOCK because Valgrind will
868 * already intercept this mmap call. However, for consistency between
869 * all the mmap paths, we mark the pointer as defined now and mark it
870 * as inaccessible afterwards.
872 VG_DEFINED(map
, bo
->size
);
874 if (p_atomic_cmpxchg(&bo
->map_gtt
, NULL
, map
)) {
875 VG_NOACCESS(map
, bo
->size
);
876 drm_munmap(map
, bo
->size
);
881 DBG("bo_map_gtt: %d (%s) -> %p, ", bo
->gem_handle
, bo
->name
, bo
->map_gtt
);
884 if (!(flags
& MAP_ASYNC
)) {
885 bo_wait_with_stall_warning(brw
, bo
, "GTT mapping");
892 can_map_cpu(struct brw_bo
*bo
, unsigned flags
)
894 if (bo
->cache_coherent
)
897 /* Even if the buffer itself is not cache-coherent (such as a scanout), on
898 * an LLC platform reads always are coherent (as they are performed via the
899 * central system agent). It is just the writes that we need to take special
900 * care to ensure that land in main memory and not stick in the CPU cache.
902 if (!(flags
& MAP_WRITE
) && bo
->bufmgr
->has_llc
)
905 /* If PERSISTENT or COHERENT are set, the mmapping needs to remain valid
906 * across batch flushes where the kernel will change cache domains of the
907 * bo, invalidating continued access to the CPU mmap on non-LLC device.
909 * Similarly, ASYNC typically means that the buffer will be accessed via
910 * both the CPU and the GPU simultaneously. Batches may be executed that
911 * use the BO even while it is mapped. While OpenGL technically disallows
912 * most drawing while non-persistent mappings are active, we may still use
913 * the GPU for blits or other operations, causing batches to happen at
914 * inconvenient times.
916 if (flags
& (MAP_PERSISTENT
| MAP_COHERENT
| MAP_ASYNC
))
919 return !(flags
& MAP_WRITE
);
923 brw_bo_map(struct brw_context
*brw
, struct brw_bo
*bo
, unsigned flags
)
925 if (bo
->tiling_mode
!= I915_TILING_NONE
&& !(flags
& MAP_RAW
))
926 return brw_bo_map_gtt(brw
, bo
, flags
);
930 if (can_map_cpu(bo
, flags
))
931 map
= brw_bo_map_cpu(brw
, bo
, flags
);
933 map
= brw_bo_map_wc(brw
, bo
, flags
);
935 /* Allow the attempt to fail by falling back to the GTT where necessary.
937 * Not every buffer can be mmaped directly using the CPU (or WC), for
938 * example buffers that wrap stolen memory or are imported from other
939 * devices. For those, we have little choice but to use a GTT mmapping.
940 * However, if we use a slow GTT mmapping for reads where we expected fast
941 * access, that order of magnitude difference in throughput will be clearly
942 * expressed by angry users.
944 * We skip MAP_RAW because we want to avoid map_gtt's fence detiling.
946 if (!map
&& !(flags
& MAP_RAW
)) {
948 perf_debug("Fallback GTT mapping for %s with access flags %x\n",
951 map
= brw_bo_map_gtt(brw
, bo
, flags
);
958 brw_bo_subdata(struct brw_bo
*bo
, uint64_t offset
,
959 uint64_t size
, const void *data
)
961 struct brw_bufmgr
*bufmgr
= bo
->bufmgr
;
962 struct drm_i915_gem_pwrite pwrite
;
966 pwrite
.handle
= bo
->gem_handle
;
967 pwrite
.offset
= offset
;
969 pwrite
.data_ptr
= (uint64_t) (uintptr_t) data
;
970 ret
= drmIoctl(bufmgr
->fd
, DRM_IOCTL_I915_GEM_PWRITE
, &pwrite
);
973 DBG("%s:%d: Error writing data to buffer %d: "
974 "(%"PRIu64
" %"PRIu64
") %s .\n",
975 __FILE__
, __LINE__
, bo
->gem_handle
, offset
, size
, strerror(errno
));
981 /** Waits for all GPU rendering with the object to have completed. */
983 brw_bo_wait_rendering(struct brw_bo
*bo
)
985 /* We require a kernel recent enough for WAIT_IOCTL support.
986 * See intel_init_bufmgr()
992 * Waits on a BO for the given amount of time.
994 * @bo: buffer object to wait for
995 * @timeout_ns: amount of time to wait in nanoseconds.
996 * If value is less than 0, an infinite wait will occur.
998 * Returns 0 if the wait was successful ie. the last batch referencing the
999 * object has completed within the allotted time. Otherwise some negative return
1000 * value describes the error. Of particular interest is -ETIME when the wait has
1001 * failed to yield the desired result.
1003 * Similar to brw_bo_wait_rendering except a timeout parameter allows
1004 * the operation to give up after a certain amount of time. Another subtle
1005 * difference is the internal locking semantics are different (this variant does
1006 * not hold the lock for the duration of the wait). This makes the wait subject
1007 * to a larger userspace race window.
1009 * The implementation shall wait until the object is no longer actively
1010 * referenced within a batch buffer at the time of the call. The wait will
1011 * not guarantee that the buffer is re-issued via another thread, or an flinked
1012 * handle. Userspace must make sure this race does not occur if such precision
1015 * Note that some kernels have broken the inifite wait for negative values
1016 * promise, upgrade to latest stable kernels if this is the case.
1019 brw_bo_wait(struct brw_bo
*bo
, int64_t timeout_ns
)
1021 struct brw_bufmgr
*bufmgr
= bo
->bufmgr
;
1022 struct drm_i915_gem_wait wait
;
1025 /* If we know it's idle, don't bother with the kernel round trip */
1026 if (bo
->idle
&& !bo
->external
)
1030 wait
.bo_handle
= bo
->gem_handle
;
1031 wait
.timeout_ns
= timeout_ns
;
1032 ret
= drmIoctl(bufmgr
->fd
, DRM_IOCTL_I915_GEM_WAIT
, &wait
);
1042 brw_bufmgr_destroy(struct brw_bufmgr
*bufmgr
)
1044 mtx_destroy(&bufmgr
->lock
);
1046 /* Free any cached buffer objects we were going to reuse */
1047 for (int i
= 0; i
< bufmgr
->num_buckets
; i
++) {
1048 struct bo_cache_bucket
*bucket
= &bufmgr
->cache_bucket
[i
];
1050 list_for_each_entry_safe(struct brw_bo
, bo
, &bucket
->head
, head
) {
1051 list_del(&bo
->head
);
1057 _mesa_hash_table_destroy(bufmgr
->name_table
, NULL
);
1058 _mesa_hash_table_destroy(bufmgr
->handle_table
, NULL
);
1064 bo_set_tiling_internal(struct brw_bo
*bo
, uint32_t tiling_mode
,
1067 struct brw_bufmgr
*bufmgr
= bo
->bufmgr
;
1068 struct drm_i915_gem_set_tiling set_tiling
;
1071 if (bo
->global_name
== 0 &&
1072 tiling_mode
== bo
->tiling_mode
&& stride
== bo
->stride
)
1075 memset(&set_tiling
, 0, sizeof(set_tiling
));
1077 /* set_tiling is slightly broken and overwrites the
1078 * input on the error path, so we have to open code
1081 set_tiling
.handle
= bo
->gem_handle
;
1082 set_tiling
.tiling_mode
= tiling_mode
;
1083 set_tiling
.stride
= stride
;
1085 ret
= ioctl(bufmgr
->fd
, DRM_IOCTL_I915_GEM_SET_TILING
, &set_tiling
);
1086 } while (ret
== -1 && (errno
== EINTR
|| errno
== EAGAIN
));
1090 bo
->tiling_mode
= set_tiling
.tiling_mode
;
1091 bo
->swizzle_mode
= set_tiling
.swizzle_mode
;
1092 bo
->stride
= set_tiling
.stride
;
1097 brw_bo_get_tiling(struct brw_bo
*bo
, uint32_t *tiling_mode
,
1098 uint32_t *swizzle_mode
)
1100 *tiling_mode
= bo
->tiling_mode
;
1101 *swizzle_mode
= bo
->swizzle_mode
;
1106 brw_bo_gem_create_from_prime(struct brw_bufmgr
*bufmgr
, int prime_fd
)
1111 struct drm_i915_gem_get_tiling get_tiling
;
1113 mtx_lock(&bufmgr
->lock
);
1114 ret
= drmPrimeFDToHandle(bufmgr
->fd
, prime_fd
, &handle
);
1116 DBG("create_from_prime: failed to obtain handle from fd: %s\n",
1118 mtx_unlock(&bufmgr
->lock
);
1123 * See if the kernel has already returned this buffer to us. Just as
1124 * for named buffers, we must not create two bo's pointing at the same
1127 bo
= hash_find_bo(bufmgr
->handle_table
, handle
);
1129 brw_bo_reference(bo
);
1133 bo
= calloc(1, sizeof(*bo
));
1137 p_atomic_set(&bo
->refcount
, 1);
1139 /* Determine size of bo. The fd-to-handle ioctl really should
1140 * return the size, but it doesn't. If we have kernel 3.12 or
1141 * later, we can lseek on the prime fd to get the size. Older
1142 * kernels will just fail, in which case we fall back to the
1143 * provided (estimated or guess size). */
1144 ret
= lseek(prime_fd
, 0, SEEK_END
);
1148 bo
->bufmgr
= bufmgr
;
1150 bo
->gem_handle
= handle
;
1151 _mesa_hash_table_insert(bufmgr
->handle_table
, &bo
->gem_handle
, bo
);
1154 bo
->reusable
= false;
1155 bo
->external
= true;
1157 memclear(get_tiling
);
1158 get_tiling
.handle
= bo
->gem_handle
;
1159 if (drmIoctl(bufmgr
->fd
, DRM_IOCTL_I915_GEM_GET_TILING
, &get_tiling
))
1162 bo
->tiling_mode
= get_tiling
.tiling_mode
;
1163 bo
->swizzle_mode
= get_tiling
.swizzle_mode
;
1164 /* XXX stride is unknown */
1167 mtx_unlock(&bufmgr
->lock
);
1172 mtx_unlock(&bufmgr
->lock
);
1177 brw_bo_gem_export_to_prime(struct brw_bo
*bo
, int *prime_fd
)
1179 struct brw_bufmgr
*bufmgr
= bo
->bufmgr
;
1181 if (drmPrimeHandleToFD(bufmgr
->fd
, bo
->gem_handle
,
1182 DRM_CLOEXEC
, prime_fd
) != 0)
1185 bo
->reusable
= false;
1186 bo
->external
= true;
1192 brw_bo_flink(struct brw_bo
*bo
, uint32_t *name
)
1194 struct brw_bufmgr
*bufmgr
= bo
->bufmgr
;
1196 if (!bo
->global_name
) {
1197 struct drm_gem_flink flink
;
1200 flink
.handle
= bo
->gem_handle
;
1201 if (drmIoctl(bufmgr
->fd
, DRM_IOCTL_GEM_FLINK
, &flink
))
1204 mtx_lock(&bufmgr
->lock
);
1205 if (!bo
->global_name
) {
1206 bo
->global_name
= flink
.name
;
1207 bo
->reusable
= false;
1208 bo
->external
= true;
1210 _mesa_hash_table_insert(bufmgr
->name_table
, &bo
->global_name
, bo
);
1212 mtx_unlock(&bufmgr
->lock
);
1215 *name
= bo
->global_name
;
1220 * Enables unlimited caching of buffer objects for reuse.
1222 * This is potentially very memory expensive, as the cache at each bucket
1223 * size is only bounded by how many buffers of that size we've managed to have
1224 * in flight at once.
1227 brw_bufmgr_enable_reuse(struct brw_bufmgr
*bufmgr
)
1229 bufmgr
->bo_reuse
= true;
1233 add_bucket(struct brw_bufmgr
*bufmgr
, int size
)
1235 unsigned int i
= bufmgr
->num_buckets
;
1237 assert(i
< ARRAY_SIZE(bufmgr
->cache_bucket
));
1239 list_inithead(&bufmgr
->cache_bucket
[i
].head
);
1240 bufmgr
->cache_bucket
[i
].size
= size
;
1241 bufmgr
->num_buckets
++;
1245 init_cache_buckets(struct brw_bufmgr
*bufmgr
)
1247 uint64_t size
, cache_max_size
= 64 * 1024 * 1024;
1249 /* OK, so power of two buckets was too wasteful of memory.
1250 * Give 3 other sizes between each power of two, to hopefully
1251 * cover things accurately enough. (The alternative is
1252 * probably to just go for exact matching of sizes, and assume
1253 * that for things like composited window resize the tiled
1254 * width/height alignment and rounding of sizes to pages will
1255 * get us useful cache hit rates anyway)
1257 add_bucket(bufmgr
, 4096);
1258 add_bucket(bufmgr
, 4096 * 2);
1259 add_bucket(bufmgr
, 4096 * 3);
1261 /* Initialize the linked lists for BO reuse cache. */
1262 for (size
= 4 * 4096; size
<= cache_max_size
; size
*= 2) {
1263 add_bucket(bufmgr
, size
);
1265 add_bucket(bufmgr
, size
+ size
* 1 / 4);
1266 add_bucket(bufmgr
, size
+ size
* 2 / 4);
1267 add_bucket(bufmgr
, size
+ size
* 3 / 4);
1272 brw_create_hw_context(struct brw_bufmgr
*bufmgr
)
1274 struct drm_i915_gem_context_create create
;
1278 ret
= drmIoctl(bufmgr
->fd
, DRM_IOCTL_I915_GEM_CONTEXT_CREATE
, &create
);
1280 DBG("DRM_IOCTL_I915_GEM_CONTEXT_CREATE failed: %s\n", strerror(errno
));
1284 return create
.ctx_id
;
1288 brw_destroy_hw_context(struct brw_bufmgr
*bufmgr
, uint32_t ctx_id
)
1290 struct drm_i915_gem_context_destroy d
= {.ctx_id
= ctx_id
};
1293 drmIoctl(bufmgr
->fd
, DRM_IOCTL_I915_GEM_CONTEXT_DESTROY
, &d
) != 0) {
1294 fprintf(stderr
, "DRM_IOCTL_I915_GEM_CONTEXT_DESTROY failed: %s\n",
1300 brw_reg_read(struct brw_bufmgr
*bufmgr
, uint32_t offset
, uint64_t *result
)
1302 struct drm_i915_reg_read reg_read
;
1306 reg_read
.offset
= offset
;
1308 ret
= drmIoctl(bufmgr
->fd
, DRM_IOCTL_I915_REG_READ
, ®_read
);
1310 *result
= reg_read
.val
;
1315 gem_param(int fd
, int name
)
1317 drm_i915_getparam_t gp
;
1318 int v
= -1; /* No param uses (yet) the sign bit, reserve it for errors */
1320 memset(&gp
, 0, sizeof(gp
));
1323 if (drmIoctl(fd
, DRM_IOCTL_I915_GETPARAM
, &gp
))
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
)
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 (mtx_init(&bufmgr
->lock
, mtx_plain
) != 0) {
1360 bufmgr
->has_llc
= devinfo
->has_llc
;
1361 bufmgr
->has_mmap_wc
= gem_param(fd
, I915_PARAM_MMAP_VERSION
) > 0;
1363 init_cache_buckets(bufmgr
);
1365 bufmgr
->name_table
=
1366 _mesa_hash_table_create(NULL
, key_hash_uint
, key_uint_equal
);
1367 bufmgr
->handle_table
=
1368 _mesa_hash_table_create(NULL
, key_hash_uint
, key_uint_equal
);