2 * Copyright © 2017 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included
12 * in all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 * DEALINGS IN THE SOFTWARE.
26 * The Iris buffer manager.
28 * XXX: write better comments
31 * - main interface to GEM in the kernel
39 #include <util/u_atomic.h>
46 #include <sys/ioctl.h>
49 #include <sys/types.h>
55 #define ETIME ETIMEDOUT
57 #include "common/gen_clflush.h"
58 #include "common/gen_debug.h"
59 #include "common/gen_gem.h"
60 #include "dev/gen_device_info.h"
61 #include "main/macros.h"
62 #include "util/debug.h"
63 #include "util/macros.h"
64 #include "util/hash_table.h"
65 #include "util/list.h"
66 #include "util/u_dynarray.h"
68 #include "iris_bufmgr.h"
69 #include "iris_context.h"
72 #include "drm-uapi/i915_drm.h"
82 /* VALGRIND_FREELIKE_BLOCK unfortunately does not actually undo the earlier
83 * VALGRIND_MALLOCLIKE_BLOCK but instead leaves vg convinced the memory is
84 * leaked. All because it does not call VG(cli_free) from its
85 * VG_USERREQ__FREELIKE_BLOCK handler. Instead of treating the memory like
86 * and allocation, we mark it available for use upon mmapping and remove
89 #define VG_DEFINED(ptr, size) VG(VALGRIND_MAKE_MEM_DEFINED(ptr, size))
90 #define VG_NOACCESS(ptr, size) VG(VALGRIND_MAKE_MEM_NOACCESS(ptr, size))
92 #define PAGE_SIZE 4096
94 #define FILE_DEBUG_FLAG DEBUG_BUFMGR
97 * Call ioctl, restarting if it is interupted
100 drm_ioctl(int fd
, unsigned long request
, void *arg
)
105 ret
= ioctl(fd
, request
, arg
);
106 } while (ret
== -1 && (errno
== EINTR
|| errno
== EAGAIN
));
111 atomic_add_unless(int *v
, int add
, int unless
)
114 c
= p_atomic_read(v
);
115 while (c
!= unless
&& (old
= p_atomic_cmpxchg(v
, c
, c
+ add
)) != c
)
121 * Iris fixed-size bucketing VMA allocator.
123 * The BO cache maintains "cache buckets" for buffers of various sizes.
124 * All buffers in a given bucket are identically sized - when allocating,
125 * we always round up to the bucket size. This means that virtually all
126 * allocations are fixed-size; only buffers which are too large to fit in
127 * a bucket can be variably-sized.
129 * We create an allocator for each bucket. Each contains a free-list, where
130 * each node contains a <starting address, 64-bit bitmap> pair. Each bit
131 * represents a bucket-sized block of memory. (At the first level, each
132 * bit corresponds to a page. For the second bucket, bits correspond to
133 * two pages, and so on.) 1 means a block is free, and 0 means it's in-use.
134 * The lowest bit in the bitmap is for the first block.
136 * This makes allocations cheap - any bit of any node will do. We can pick
137 * the head of the list and use ffs() to find a free block. If there are
138 * none, we allocate 64 blocks from a larger allocator - either a bigger
139 * bucketing allocator, or a fallback top-level allocator for large objects.
141 struct vma_bucket_node
{
142 uint64_t start_address
;
146 struct bo_cache_bucket
{
147 /** List of cached BOs. */
148 struct list_head head
;
150 /** Size of this bucket, in bytes. */
153 /** List of vma_bucket_nodes. */
154 struct util_dynarray vma_list
[IRIS_MEMZONE_COUNT
];
162 /** Array of lists of cached gem objects of power-of-two sizes */
163 struct bo_cache_bucket cache_bucket
[14 * 4];
167 struct hash_table
*name_table
;
168 struct hash_table
*handle_table
;
170 struct util_vma_heap vma_allocator
[IRIS_MEMZONE_COUNT
];
176 static int bo_set_tiling_internal(struct iris_bo
*bo
, uint32_t tiling_mode
,
179 static void bo_free(struct iris_bo
*bo
);
181 static uint64_t vma_alloc(struct iris_bufmgr
*bufmgr
,
182 enum iris_memory_zone memzone
,
183 uint64_t size
, uint64_t alignment
);
186 key_hash_uint(const void *key
)
188 return _mesa_hash_data(key
, 4);
192 key_uint_equal(const void *a
, const void *b
)
194 return *((unsigned *) a
) == *((unsigned *) b
);
197 static struct iris_bo
*
198 hash_find_bo(struct hash_table
*ht
, unsigned int key
)
200 struct hash_entry
*entry
= _mesa_hash_table_search(ht
, &key
);
201 return entry
? (struct iris_bo
*) entry
->data
: NULL
;
205 * This function finds the correct bucket fit for the input size.
206 * The function works with O(1) complexity when the requested size
207 * was queried instead of iterating the size through all the buckets.
209 static struct bo_cache_bucket
*
210 bucket_for_size(struct iris_bufmgr
*bufmgr
, uint64_t size
)
212 /* Calculating the pages and rounding up to the page size. */
213 const unsigned pages
= (size
+ PAGE_SIZE
- 1) / PAGE_SIZE
;
215 /* Row Bucket sizes clz((x-1) | 3) Row Column
216 * in pages stride size
217 * 0: 1 2 3 4 -> 30 30 30 30 4 1
218 * 1: 5 6 7 8 -> 29 29 29 29 4 1
219 * 2: 10 12 14 16 -> 28 28 28 28 8 2
220 * 3: 20 24 28 32 -> 27 27 27 27 16 4
222 const unsigned row
= 30 - __builtin_clz((pages
- 1) | 3);
223 const unsigned row_max_pages
= 4 << row
;
225 /* The '& ~2' is the special case for row 1. In row 1, max pages /
226 * 2 is 2, but the previous row maximum is zero (because there is
227 * no previous row). All row maximum sizes are power of 2, so that
228 * is the only case where that bit will be set.
230 const unsigned prev_row_max_pages
= (row_max_pages
/ 2) & ~2;
231 int col_size_log2
= row
- 1;
232 col_size_log2
+= (col_size_log2
< 0);
234 const unsigned col
= (pages
- prev_row_max_pages
+
235 ((1 << col_size_log2
) - 1)) >> col_size_log2
;
237 /* Calculating the index based on the row and column. */
238 const unsigned index
= (row
* 4) + (col
- 1);
240 return (index
< bufmgr
->num_buckets
) ?
241 &bufmgr
->cache_bucket
[index
] : NULL
;
244 static enum iris_memory_zone
245 memzone_for_address(uint64_t address
)
247 STATIC_ASSERT(IRIS_MEMZONE_OTHER_START
> IRIS_MEMZONE_DYNAMIC_START
);
248 STATIC_ASSERT(IRIS_MEMZONE_DYNAMIC_START
> IRIS_MEMZONE_SURFACE_START
);
249 STATIC_ASSERT(IRIS_MEMZONE_SURFACE_START
> IRIS_MEMZONE_BINDER_START
);
250 STATIC_ASSERT(IRIS_MEMZONE_BINDER_START
> IRIS_MEMZONE_SHADER_START
);
251 STATIC_ASSERT(IRIS_BORDER_COLOR_POOL_ADDRESS
== IRIS_MEMZONE_DYNAMIC_START
);
253 if (address
>= IRIS_MEMZONE_OTHER_START
)
254 return IRIS_MEMZONE_OTHER
;
256 if (address
== IRIS_BORDER_COLOR_POOL_ADDRESS
)
257 return IRIS_MEMZONE_BORDER_COLOR_POOL
;
259 if (address
> IRIS_MEMZONE_DYNAMIC_START
)
260 return IRIS_MEMZONE_DYNAMIC
;
262 if (address
> IRIS_MEMZONE_BINDER_START
)
263 return IRIS_MEMZONE_BINDER
;
265 if (address
> IRIS_MEMZONE_SURFACE_START
)
266 return IRIS_MEMZONE_SURFACE
;
268 return IRIS_MEMZONE_SHADER
;
272 bucket_vma_alloc(struct iris_bufmgr
*bufmgr
,
273 struct bo_cache_bucket
*bucket
,
274 enum iris_memory_zone memzone
)
276 struct util_dynarray
*vma_list
= &bucket
->vma_list
[memzone
];
277 struct vma_bucket_node
*node
;
279 if (vma_list
->size
== 0) {
280 /* This bucket allocator is out of space - allocate a new block of
281 * memory for 64 blocks from a larger allocator (either a larger
282 * bucket or util_vma).
284 * We align the address to the node size (64 blocks) so that
285 * bucket_vma_free can easily compute the starting address of this
286 * block by rounding any address we return down to the node size.
288 * Set the first bit used, and return the start address.
290 const uint64_t node_size
= 64ull * bucket
->size
;
291 node
= util_dynarray_grow(vma_list
, sizeof(struct vma_bucket_node
));
296 uint64_t addr
= vma_alloc(bufmgr
, memzone
, node_size
, node_size
);
297 node
->start_address
= gen_48b_address(addr
);
298 node
->bitmap
= ~1ull;
299 return node
->start_address
;
302 /* Pick any bit from any node - they're all the right size and free. */
303 node
= util_dynarray_top_ptr(vma_list
, struct vma_bucket_node
);
304 int bit
= ffsll(node
->bitmap
) - 1;
305 assert(bit
>= 0 && bit
<= 63);
307 /* Reserve the memory by clearing the bit. */
308 assert((node
->bitmap
& (1ull << bit
)) != 0ull);
309 node
->bitmap
&= ~(1ull << bit
);
311 uint64_t addr
= node
->start_address
+ bit
* bucket
->size
;
313 /* If this node is now completely full, remove it from the free list. */
314 if (node
->bitmap
== 0ull) {
315 (void) util_dynarray_pop(vma_list
, struct vma_bucket_node
);
322 bucket_vma_free(struct bo_cache_bucket
*bucket
, uint64_t address
)
324 enum iris_memory_zone memzone
= memzone_for_address(address
);
325 struct util_dynarray
*vma_list
= &bucket
->vma_list
[memzone
];
326 const uint64_t node_bytes
= 64ull * bucket
->size
;
327 struct vma_bucket_node
*node
= NULL
;
329 /* bucket_vma_alloc allocates 64 blocks at a time, and aligns it to
330 * that 64 block size. So, we can round down to get the starting address.
332 uint64_t start
= (address
/ node_bytes
) * node_bytes
;
334 /* Dividing the offset from start by bucket size gives us the bit index. */
335 int bit
= (address
- start
) / bucket
->size
;
337 assert(start
+ bit
* bucket
->size
== address
);
339 util_dynarray_foreach(vma_list
, struct vma_bucket_node
, cur
) {
340 if (cur
->start_address
== start
) {
347 /* No node - the whole group of 64 blocks must have been in-use. */
348 node
= util_dynarray_grow(vma_list
, sizeof(struct vma_bucket_node
));
351 return; /* bogus, leaks some GPU VMA, but nothing we can do... */
353 node
->start_address
= start
;
357 /* Set the bit to return the memory. */
358 assert((node
->bitmap
& (1ull << bit
)) == 0ull);
359 node
->bitmap
|= 1ull << bit
;
361 /* The block might be entirely free now, and if so, we could return it
362 * to the larger allocator. But we may as well hang on to it, in case
363 * we get more allocations at this block size.
367 static struct bo_cache_bucket
*
368 get_bucket_allocator(struct iris_bufmgr
*bufmgr
,
369 enum iris_memory_zone memzone
,
372 /* Bucketing is not worth using for binders...we'll never have 64... */
373 if (memzone
== IRIS_MEMZONE_BINDER
)
376 /* Skip using the bucket allocator for very large sizes, as it allocates
377 * 64 of them and this can balloon rather quickly.
379 if (size
> 1024 * PAGE_SIZE
)
382 struct bo_cache_bucket
*bucket
= bucket_for_size(bufmgr
, size
);
384 if (bucket
&& bucket
->size
== size
)
391 * Allocate a section of virtual memory for a buffer, assigning an address.
393 * This uses either the bucket allocator for the given size, or the large
394 * object allocator (util_vma).
397 vma_alloc(struct iris_bufmgr
*bufmgr
,
398 enum iris_memory_zone memzone
,
402 if (memzone
== IRIS_MEMZONE_BORDER_COLOR_POOL
)
403 return IRIS_BORDER_COLOR_POOL_ADDRESS
;
405 struct bo_cache_bucket
*bucket
=
406 get_bucket_allocator(bufmgr
, memzone
, size
);
410 addr
= bucket_vma_alloc(bufmgr
, bucket
, memzone
);
412 addr
= util_vma_heap_alloc(&bufmgr
->vma_allocator
[memzone
], size
,
416 assert((addr
>> 48ull) == 0);
417 assert((addr
% alignment
) == 0);
419 return gen_canonical_address(addr
);
423 vma_free(struct iris_bufmgr
*bufmgr
,
427 if (address
== IRIS_BORDER_COLOR_POOL_ADDRESS
)
430 /* Un-canonicalize the address. */
431 address
= gen_48b_address(address
);
436 enum iris_memory_zone memzone
= memzone_for_address(address
);
437 struct bo_cache_bucket
*bucket
=
438 get_bucket_allocator(bufmgr
, memzone
, size
);
441 bucket_vma_free(bucket
, address
);
443 util_vma_heap_free(&bufmgr
->vma_allocator
[memzone
], address
, size
);
448 iris_bo_busy(struct iris_bo
*bo
)
450 struct iris_bufmgr
*bufmgr
= bo
->bufmgr
;
451 struct drm_i915_gem_busy busy
= { .handle
= bo
->gem_handle
};
453 int ret
= drm_ioctl(bufmgr
->fd
, DRM_IOCTL_I915_GEM_BUSY
, &busy
);
455 bo
->idle
= !busy
.busy
;
462 iris_bo_madvise(struct iris_bo
*bo
, int state
)
464 struct drm_i915_gem_madvise madv
= {
465 .handle
= bo
->gem_handle
,
470 drm_ioctl(bo
->bufmgr
->fd
, DRM_IOCTL_I915_GEM_MADVISE
, &madv
);
472 return madv
.retained
;
475 /* drop the oldest entries that have been purged by the kernel */
477 iris_bo_cache_purge_bucket(struct iris_bufmgr
*bufmgr
,
478 struct bo_cache_bucket
*bucket
)
480 list_for_each_entry_safe(struct iris_bo
, bo
, &bucket
->head
, head
) {
481 if (iris_bo_madvise(bo
, I915_MADV_DONTNEED
))
489 static struct iris_bo
*
492 struct iris_bo
*bo
= calloc(1, sizeof(*bo
));
494 bo
->hash
= _mesa_hash_pointer(bo
);
499 static struct iris_bo
*
500 bo_alloc_internal(struct iris_bufmgr
*bufmgr
,
503 enum iris_memory_zone memzone
,
505 uint32_t tiling_mode
,
509 unsigned int page_size
= getpagesize();
511 struct bo_cache_bucket
*bucket
;
512 bool alloc_from_cache
;
516 if (flags
& BO_ALLOC_ZEROED
)
519 /* Round the allocated size up to a power of two number of pages. */
520 bucket
= bucket_for_size(bufmgr
, size
);
522 /* If we don't have caching at this size, don't actually round the
525 if (bucket
== NULL
) {
526 bo_size
= MAX2(ALIGN(size
, page_size
), page_size
);
528 bo_size
= bucket
->size
;
531 mtx_lock(&bufmgr
->lock
);
532 /* Get a buffer out of the cache if available */
534 alloc_from_cache
= false;
535 if (bucket
!= NULL
&& !list_empty(&bucket
->head
)) {
536 /* If the last BO in the cache is idle, then reuse it. Otherwise,
537 * allocate a fresh buffer to avoid stalling.
539 bo
= LIST_ENTRY(struct iris_bo
, bucket
->head
.next
, head
);
540 if (!iris_bo_busy(bo
)) {
541 alloc_from_cache
= true;
545 if (alloc_from_cache
) {
546 if (!iris_bo_madvise(bo
, I915_MADV_WILLNEED
)) {
548 iris_bo_cache_purge_bucket(bufmgr
, bucket
);
552 if (bo_set_tiling_internal(bo
, tiling_mode
, stride
)) {
558 void *map
= iris_bo_map(NULL
, bo
, MAP_WRITE
| MAP_RAW
);
563 memset(map
, 0, bo_size
);
568 if (alloc_from_cache
) {
569 /* If the cached BO isn't in the right memory zone, free the old
570 * memory and assign it a new address.
572 if (memzone
!= memzone_for_address(bo
->gtt_offset
)) {
573 vma_free(bufmgr
, bo
->gtt_offset
, bo_size
);
574 bo
->gtt_offset
= 0ull;
584 struct drm_i915_gem_create create
= { .size
= bo_size
};
586 /* All new BOs we get from the kernel are zeroed, so we don't need to
587 * worry about that here.
589 ret
= drm_ioctl(bufmgr
->fd
, DRM_IOCTL_I915_GEM_CREATE
, &create
);
595 bo
->gem_handle
= create
.handle
;
599 bo
->tiling_mode
= I915_TILING_NONE
;
600 bo
->swizzle_mode
= I915_BIT_6_SWIZZLE_NONE
;
603 if (bo_set_tiling_internal(bo
, tiling_mode
, stride
))
606 /* Calling set_domain() will allocate pages for the BO outside of the
607 * struct mutex lock in the kernel, which is more efficient than waiting
608 * to create them during the first execbuf that uses the BO.
610 struct drm_i915_gem_set_domain sd
= {
611 .handle
= bo
->gem_handle
,
612 .read_domains
= I915_GEM_DOMAIN_CPU
,
616 if (drm_ioctl(bo
->bufmgr
->fd
, DRM_IOCTL_I915_GEM_SET_DOMAIN
, &sd
) != 0)
621 p_atomic_set(&bo
->refcount
, 1);
622 bo
->reusable
= bucket
&& bufmgr
->bo_reuse
;
623 bo
->cache_coherent
= bufmgr
->has_llc
;
625 bo
->kflags
= EXEC_OBJECT_SUPPORTS_48B_ADDRESS
| EXEC_OBJECT_PINNED
;
627 /* By default, capture all driver-internal buffers like shader kernels,
628 * surface states, dynamic states, border colors, and so on.
630 if (memzone
< IRIS_MEMZONE_OTHER
)
631 bo
->kflags
|= EXEC_OBJECT_CAPTURE
;
633 if (bo
->gtt_offset
== 0ull) {
634 bo
->gtt_offset
= vma_alloc(bufmgr
, memzone
, bo
->size
, 1);
636 if (bo
->gtt_offset
== 0ull)
640 mtx_unlock(&bufmgr
->lock
);
642 DBG("bo_create: buf %d (%s) %llub\n", bo
->gem_handle
, bo
->name
,
643 (unsigned long long) size
);
650 mtx_unlock(&bufmgr
->lock
);
655 iris_bo_alloc(struct iris_bufmgr
*bufmgr
,
658 enum iris_memory_zone memzone
)
660 return bo_alloc_internal(bufmgr
, name
, size
, memzone
,
661 0, I915_TILING_NONE
, 0);
665 iris_bo_alloc_tiled(struct iris_bufmgr
*bufmgr
, const char *name
,
666 uint64_t size
, enum iris_memory_zone memzone
,
667 uint32_t tiling_mode
, uint32_t pitch
, unsigned flags
)
669 return bo_alloc_internal(bufmgr
, name
, size
, memzone
,
670 flags
, tiling_mode
, pitch
);
674 iris_bo_create_userptr(struct iris_bufmgr
*bufmgr
, const char *name
,
675 void *ptr
, size_t size
,
676 enum iris_memory_zone memzone
)
684 struct drm_i915_gem_userptr arg
= {
685 .user_ptr
= (uintptr_t)ptr
,
688 if (drm_ioctl(bufmgr
->fd
, DRM_IOCTL_I915_GEM_USERPTR
, &arg
))
690 bo
->gem_handle
= arg
.handle
;
692 /* Check the buffer for validity before we try and use it in a batch */
693 struct drm_i915_gem_set_domain sd
= {
694 .handle
= bo
->gem_handle
,
695 .read_domains
= I915_GEM_DOMAIN_CPU
,
697 if (drm_ioctl(bufmgr
->fd
, DRM_IOCTL_I915_GEM_SET_DOMAIN
, &sd
))
705 bo
->kflags
= EXEC_OBJECT_SUPPORTS_48B_ADDRESS
| EXEC_OBJECT_PINNED
;
706 bo
->gtt_offset
= vma_alloc(bufmgr
, memzone
, size
, 1);
707 if (bo
->gtt_offset
== 0ull)
710 p_atomic_set(&bo
->refcount
, 1);
712 bo
->cache_coherent
= true;
719 drm_ioctl(bufmgr
->fd
, DRM_IOCTL_GEM_CLOSE
, &bo
->gem_handle
);
726 * Returns a iris_bo wrapping the given buffer object handle.
728 * This can be used when one application needs to pass a buffer object
732 iris_bo_gem_create_from_name(struct iris_bufmgr
*bufmgr
,
733 const char *name
, unsigned int handle
)
737 /* At the moment most applications only have a few named bo.
738 * For instance, in a DRI client only the render buffers passed
739 * between X and the client are named. And since X returns the
740 * alternating names for the front/back buffer a linear search
741 * provides a sufficiently fast match.
743 mtx_lock(&bufmgr
->lock
);
744 bo
= hash_find_bo(bufmgr
->name_table
, handle
);
746 iris_bo_reference(bo
);
750 struct drm_gem_open open_arg
= { .name
= handle
};
751 int ret
= drm_ioctl(bufmgr
->fd
, DRM_IOCTL_GEM_OPEN
, &open_arg
);
753 DBG("Couldn't reference %s handle 0x%08x: %s\n",
754 name
, handle
, strerror(errno
));
758 /* Now see if someone has used a prime handle to get this
759 * object from the kernel before by looking through the list
760 * again for a matching gem_handle
762 bo
= hash_find_bo(bufmgr
->handle_table
, open_arg
.handle
);
764 iris_bo_reference(bo
);
772 p_atomic_set(&bo
->refcount
, 1);
774 bo
->size
= open_arg
.size
;
777 bo
->gem_handle
= open_arg
.handle
;
779 bo
->global_name
= handle
;
780 bo
->reusable
= false;
782 bo
->kflags
= EXEC_OBJECT_SUPPORTS_48B_ADDRESS
| EXEC_OBJECT_PINNED
;
783 bo
->gtt_offset
= vma_alloc(bufmgr
, IRIS_MEMZONE_OTHER
, bo
->size
, 1);
785 _mesa_hash_table_insert(bufmgr
->handle_table
, &bo
->gem_handle
, bo
);
786 _mesa_hash_table_insert(bufmgr
->name_table
, &bo
->global_name
, bo
);
788 struct drm_i915_gem_get_tiling get_tiling
= { .handle
= bo
->gem_handle
};
789 ret
= drm_ioctl(bufmgr
->fd
, DRM_IOCTL_I915_GEM_GET_TILING
, &get_tiling
);
793 bo
->tiling_mode
= get_tiling
.tiling_mode
;
794 bo
->swizzle_mode
= get_tiling
.swizzle_mode
;
795 /* XXX stride is unknown */
796 DBG("bo_create_from_handle: %d (%s)\n", handle
, bo
->name
);
799 mtx_unlock(&bufmgr
->lock
);
804 mtx_unlock(&bufmgr
->lock
);
809 bo_free(struct iris_bo
*bo
)
811 struct iris_bufmgr
*bufmgr
= bo
->bufmgr
;
813 if (bo
->map_cpu
&& !bo
->userptr
) {
814 VG_NOACCESS(bo
->map_cpu
, bo
->size
);
815 munmap(bo
->map_cpu
, bo
->size
);
818 VG_NOACCESS(bo
->map_wc
, bo
->size
);
819 munmap(bo
->map_wc
, bo
->size
);
822 VG_NOACCESS(bo
->map_gtt
, bo
->size
);
823 munmap(bo
->map_gtt
, bo
->size
);
827 struct hash_entry
*entry
;
829 if (bo
->global_name
) {
830 entry
= _mesa_hash_table_search(bufmgr
->name_table
, &bo
->global_name
);
831 _mesa_hash_table_remove(bufmgr
->name_table
, entry
);
834 entry
= _mesa_hash_table_search(bufmgr
->handle_table
, &bo
->gem_handle
);
835 _mesa_hash_table_remove(bufmgr
->handle_table
, entry
);
838 /* Close this object */
839 struct drm_gem_close close
= { .handle
= bo
->gem_handle
};
840 int ret
= drm_ioctl(bufmgr
->fd
, DRM_IOCTL_GEM_CLOSE
, &close
);
842 DBG("DRM_IOCTL_GEM_CLOSE %d failed (%s): %s\n",
843 bo
->gem_handle
, bo
->name
, strerror(errno
));
846 vma_free(bo
->bufmgr
, bo
->gtt_offset
, bo
->size
);
851 /** Frees all cached buffers significantly older than @time. */
853 cleanup_bo_cache(struct iris_bufmgr
*bufmgr
, time_t time
)
857 if (bufmgr
->time
== time
)
860 for (i
= 0; i
< bufmgr
->num_buckets
; i
++) {
861 struct bo_cache_bucket
*bucket
= &bufmgr
->cache_bucket
[i
];
863 list_for_each_entry_safe(struct iris_bo
, bo
, &bucket
->head
, head
) {
864 if (time
- bo
->free_time
<= 1)
877 bo_unreference_final(struct iris_bo
*bo
, time_t time
)
879 struct iris_bufmgr
*bufmgr
= bo
->bufmgr
;
880 struct bo_cache_bucket
*bucket
;
882 DBG("bo_unreference final: %d (%s)\n", bo
->gem_handle
, bo
->name
);
886 bucket
= bucket_for_size(bufmgr
, bo
->size
);
887 /* Put the buffer into our internal cache for reuse if we can. */
888 if (bucket
&& iris_bo_madvise(bo
, I915_MADV_DONTNEED
)) {
889 bo
->free_time
= time
;
892 list_addtail(&bo
->head
, &bucket
->head
);
899 iris_bo_unreference(struct iris_bo
*bo
)
904 assert(p_atomic_read(&bo
->refcount
) > 0);
906 if (atomic_add_unless(&bo
->refcount
, -1, 1)) {
907 struct iris_bufmgr
*bufmgr
= bo
->bufmgr
;
908 struct timespec time
;
910 clock_gettime(CLOCK_MONOTONIC
, &time
);
912 mtx_lock(&bufmgr
->lock
);
914 if (p_atomic_dec_zero(&bo
->refcount
)) {
915 bo_unreference_final(bo
, time
.tv_sec
);
916 cleanup_bo_cache(bufmgr
, time
.tv_sec
);
919 mtx_unlock(&bufmgr
->lock
);
924 bo_wait_with_stall_warning(struct pipe_debug_callback
*dbg
,
928 bool busy
= dbg
&& !bo
->idle
;
929 double elapsed
= unlikely(busy
) ? -get_time() : 0.0;
931 iris_bo_wait_rendering(bo
);
933 if (unlikely(busy
)) {
934 elapsed
+= get_time();
935 if (elapsed
> 1e-5) /* 0.01ms */ {
936 perf_debug(dbg
, "%s a busy \"%s\" BO stalled and took %.03f ms.\n",
937 action
, bo
->name
, elapsed
* 1000);
943 print_flags(unsigned flags
)
945 if (flags
& MAP_READ
)
947 if (flags
& MAP_WRITE
)
949 if (flags
& MAP_ASYNC
)
951 if (flags
& MAP_PERSISTENT
)
953 if (flags
& MAP_COHERENT
)
961 iris_bo_map_cpu(struct pipe_debug_callback
*dbg
,
962 struct iris_bo
*bo
, unsigned flags
)
964 struct iris_bufmgr
*bufmgr
= bo
->bufmgr
;
966 /* We disallow CPU maps for writing to non-coherent buffers, as the
967 * CPU map can become invalidated when a batch is flushed out, which
968 * can happen at unpredictable times. You should use WC maps instead.
970 assert(bo
->cache_coherent
|| !(flags
& MAP_WRITE
));
973 DBG("iris_bo_map_cpu: %d (%s)\n", bo
->gem_handle
, bo
->name
);
975 struct drm_i915_gem_mmap mmap_arg
= {
976 .handle
= bo
->gem_handle
,
979 int ret
= drm_ioctl(bufmgr
->fd
, DRM_IOCTL_I915_GEM_MMAP
, &mmap_arg
);
981 DBG("%s:%d: Error mapping buffer %d (%s): %s .\n",
982 __FILE__
, __LINE__
, bo
->gem_handle
, bo
->name
, strerror(errno
));
985 void *map
= (void *) (uintptr_t) mmap_arg
.addr_ptr
;
986 VG_DEFINED(map
, bo
->size
);
988 if (p_atomic_cmpxchg(&bo
->map_cpu
, NULL
, map
)) {
989 VG_NOACCESS(map
, bo
->size
);
990 munmap(map
, bo
->size
);
995 DBG("iris_bo_map_cpu: %d (%s) -> %p, ", bo
->gem_handle
, bo
->name
,
999 if (!(flags
& MAP_ASYNC
)) {
1000 bo_wait_with_stall_warning(dbg
, bo
, "CPU mapping");
1003 if (!bo
->cache_coherent
&& !bo
->bufmgr
->has_llc
) {
1004 /* If we're reusing an existing CPU mapping, the CPU caches may
1005 * contain stale data from the last time we read from that mapping.
1006 * (With the BO cache, it might even be data from a previous buffer!)
1007 * Even if it's a brand new mapping, the kernel may have zeroed the
1008 * buffer via CPU writes.
1010 * We need to invalidate those cachelines so that we see the latest
1011 * contents, and so long as we only read from the CPU mmap we do not
1012 * need to write those cachelines back afterwards.
1014 * On LLC, the emprical evidence suggests that writes from the GPU
1015 * that bypass the LLC (i.e. for scanout) do *invalidate* the CPU
1016 * cachelines. (Other reads, such as the display engine, bypass the
1017 * LLC entirely requiring us to keep dirty pixels for the scanout
1018 * out of any cache.)
1020 gen_invalidate_range(bo
->map_cpu
, bo
->size
);
1027 iris_bo_map_wc(struct pipe_debug_callback
*dbg
,
1028 struct iris_bo
*bo
, unsigned flags
)
1030 struct iris_bufmgr
*bufmgr
= bo
->bufmgr
;
1033 DBG("iris_bo_map_wc: %d (%s)\n", bo
->gem_handle
, bo
->name
);
1035 struct drm_i915_gem_mmap mmap_arg
= {
1036 .handle
= bo
->gem_handle
,
1038 .flags
= I915_MMAP_WC
,
1040 int ret
= drm_ioctl(bufmgr
->fd
, DRM_IOCTL_I915_GEM_MMAP
, &mmap_arg
);
1042 DBG("%s:%d: Error mapping buffer %d (%s): %s .\n",
1043 __FILE__
, __LINE__
, bo
->gem_handle
, bo
->name
, strerror(errno
));
1047 void *map
= (void *) (uintptr_t) mmap_arg
.addr_ptr
;
1048 VG_DEFINED(map
, bo
->size
);
1050 if (p_atomic_cmpxchg(&bo
->map_wc
, NULL
, map
)) {
1051 VG_NOACCESS(map
, bo
->size
);
1052 munmap(map
, bo
->size
);
1057 DBG("iris_bo_map_wc: %d (%s) -> %p\n", bo
->gem_handle
, bo
->name
, bo
->map_wc
);
1060 if (!(flags
& MAP_ASYNC
)) {
1061 bo_wait_with_stall_warning(dbg
, bo
, "WC mapping");
1068 * Perform an uncached mapping via the GTT.
1070 * Write access through the GTT is not quite fully coherent. On low power
1071 * systems especially, like modern Atoms, we can observe reads from RAM before
1072 * the write via GTT has landed. A write memory barrier that flushes the Write
1073 * Combining Buffer (i.e. sfence/mfence) is not sufficient to order the later
1074 * read after the write as the GTT write suffers a small delay through the GTT
1075 * indirection. The kernel uses an uncached mmio read to ensure the GTT write
1076 * is ordered with reads (either by the GPU, WB or WC) and unconditionally
1077 * flushes prior to execbuf submission. However, if we are not informing the
1078 * kernel about our GTT writes, it will not flush before earlier access, such
1079 * as when using the cmdparser. Similarly, we need to be careful if we should
1080 * ever issue a CPU read immediately following a GTT write.
1082 * Telling the kernel about write access also has one more important
1083 * side-effect. Upon receiving notification about the write, it cancels any
1084 * scanout buffering for FBC/PSR and friends. Later FBC/PSR is then flushed by
1085 * either SW_FINISH or DIRTYFB. The presumption is that we never write to the
1086 * actual scanout via a mmaping, only to a backbuffer and so all the FBC/PSR
1087 * tracking is handled on the buffer exchange instead.
1090 iris_bo_map_gtt(struct pipe_debug_callback
*dbg
,
1091 struct iris_bo
*bo
, unsigned flags
)
1093 struct iris_bufmgr
*bufmgr
= bo
->bufmgr
;
1095 /* Get a mapping of the buffer if we haven't before. */
1096 if (bo
->map_gtt
== NULL
) {
1097 DBG("bo_map_gtt: mmap %d (%s)\n", bo
->gem_handle
, bo
->name
);
1099 struct drm_i915_gem_mmap_gtt mmap_arg
= { .handle
= bo
->gem_handle
};
1101 /* Get the fake offset back... */
1102 int ret
= drm_ioctl(bufmgr
->fd
, DRM_IOCTL_I915_GEM_MMAP_GTT
, &mmap_arg
);
1104 DBG("%s:%d: Error preparing buffer map %d (%s): %s .\n",
1105 __FILE__
, __LINE__
, bo
->gem_handle
, bo
->name
, strerror(errno
));
1110 void *map
= mmap(0, bo
->size
, PROT_READ
| PROT_WRITE
,
1111 MAP_SHARED
, bufmgr
->fd
, mmap_arg
.offset
);
1112 if (map
== MAP_FAILED
) {
1113 DBG("%s:%d: Error mapping buffer %d (%s): %s .\n",
1114 __FILE__
, __LINE__
, bo
->gem_handle
, bo
->name
, strerror(errno
));
1118 /* We don't need to use VALGRIND_MALLOCLIKE_BLOCK because Valgrind will
1119 * already intercept this mmap call. However, for consistency between
1120 * all the mmap paths, we mark the pointer as defined now and mark it
1121 * as inaccessible afterwards.
1123 VG_DEFINED(map
, bo
->size
);
1125 if (p_atomic_cmpxchg(&bo
->map_gtt
, NULL
, map
)) {
1126 VG_NOACCESS(map
, bo
->size
);
1127 munmap(map
, bo
->size
);
1130 assert(bo
->map_gtt
);
1132 DBG("bo_map_gtt: %d (%s) -> %p, ", bo
->gem_handle
, bo
->name
, bo
->map_gtt
);
1135 if (!(flags
& MAP_ASYNC
)) {
1136 bo_wait_with_stall_warning(dbg
, bo
, "GTT mapping");
1143 can_map_cpu(struct iris_bo
*bo
, unsigned flags
)
1145 if (bo
->cache_coherent
)
1148 /* Even if the buffer itself is not cache-coherent (such as a scanout), on
1149 * an LLC platform reads always are coherent (as they are performed via the
1150 * central system agent). It is just the writes that we need to take special
1151 * care to ensure that land in main memory and not stick in the CPU cache.
1153 if (!(flags
& MAP_WRITE
) && bo
->bufmgr
->has_llc
)
1156 /* If PERSISTENT or COHERENT are set, the mmapping needs to remain valid
1157 * across batch flushes where the kernel will change cache domains of the
1158 * bo, invalidating continued access to the CPU mmap on non-LLC device.
1160 * Similarly, ASYNC typically means that the buffer will be accessed via
1161 * both the CPU and the GPU simultaneously. Batches may be executed that
1162 * use the BO even while it is mapped. While OpenGL technically disallows
1163 * most drawing while non-persistent mappings are active, we may still use
1164 * the GPU for blits or other operations, causing batches to happen at
1165 * inconvenient times.
1167 if (flags
& (MAP_PERSISTENT
| MAP_COHERENT
| MAP_ASYNC
))
1170 return !(flags
& MAP_WRITE
);
1174 iris_bo_map(struct pipe_debug_callback
*dbg
,
1175 struct iris_bo
*bo
, unsigned flags
)
1177 if (bo
->tiling_mode
!= I915_TILING_NONE
&& !(flags
& MAP_RAW
))
1178 return iris_bo_map_gtt(dbg
, bo
, flags
);
1182 if (can_map_cpu(bo
, flags
))
1183 map
= iris_bo_map_cpu(dbg
, bo
, flags
);
1185 map
= iris_bo_map_wc(dbg
, bo
, flags
);
1187 /* Allow the attempt to fail by falling back to the GTT where necessary.
1189 * Not every buffer can be mmaped directly using the CPU (or WC), for
1190 * example buffers that wrap stolen memory or are imported from other
1191 * devices. For those, we have little choice but to use a GTT mmapping.
1192 * However, if we use a slow GTT mmapping for reads where we expected fast
1193 * access, that order of magnitude difference in throughput will be clearly
1194 * expressed by angry users.
1196 * We skip MAP_RAW because we want to avoid map_gtt's fence detiling.
1198 if (!map
&& !(flags
& MAP_RAW
)) {
1199 perf_debug(dbg
, "Fallback GTT mapping for %s with access flags %x\n",
1201 map
= iris_bo_map_gtt(dbg
, bo
, flags
);
1207 /** Waits for all GPU rendering with the object to have completed. */
1209 iris_bo_wait_rendering(struct iris_bo
*bo
)
1211 /* We require a kernel recent enough for WAIT_IOCTL support.
1212 * See intel_init_bufmgr()
1214 iris_bo_wait(bo
, -1);
1218 * Waits on a BO for the given amount of time.
1220 * @bo: buffer object to wait for
1221 * @timeout_ns: amount of time to wait in nanoseconds.
1222 * If value is less than 0, an infinite wait will occur.
1224 * Returns 0 if the wait was successful ie. the last batch referencing the
1225 * object has completed within the allotted time. Otherwise some negative return
1226 * value describes the error. Of particular interest is -ETIME when the wait has
1227 * failed to yield the desired result.
1229 * Similar to iris_bo_wait_rendering except a timeout parameter allows
1230 * the operation to give up after a certain amount of time. Another subtle
1231 * difference is the internal locking semantics are different (this variant does
1232 * not hold the lock for the duration of the wait). This makes the wait subject
1233 * to a larger userspace race window.
1235 * The implementation shall wait until the object is no longer actively
1236 * referenced within a batch buffer at the time of the call. The wait will
1237 * not guarantee that the buffer is re-issued via another thread, or an flinked
1238 * handle. Userspace must make sure this race does not occur if such precision
1241 * Note that some kernels have broken the inifite wait for negative values
1242 * promise, upgrade to latest stable kernels if this is the case.
1245 iris_bo_wait(struct iris_bo
*bo
, int64_t timeout_ns
)
1247 struct iris_bufmgr
*bufmgr
= bo
->bufmgr
;
1249 /* If we know it's idle, don't bother with the kernel round trip */
1250 if (bo
->idle
&& !bo
->external
)
1253 struct drm_i915_gem_wait wait
= {
1254 .bo_handle
= bo
->gem_handle
,
1255 .timeout_ns
= timeout_ns
,
1257 int ret
= drm_ioctl(bufmgr
->fd
, DRM_IOCTL_I915_GEM_WAIT
, &wait
);
1267 iris_bufmgr_destroy(struct iris_bufmgr
*bufmgr
)
1269 mtx_destroy(&bufmgr
->lock
);
1271 /* Free any cached buffer objects we were going to reuse */
1272 for (int i
= 0; i
< bufmgr
->num_buckets
; i
++) {
1273 struct bo_cache_bucket
*bucket
= &bufmgr
->cache_bucket
[i
];
1275 list_for_each_entry_safe(struct iris_bo
, bo
, &bucket
->head
, head
) {
1276 list_del(&bo
->head
);
1281 for (int z
= 0; z
< IRIS_MEMZONE_COUNT
; z
++)
1282 util_dynarray_fini(&bucket
->vma_list
[z
]);
1285 _mesa_hash_table_destroy(bufmgr
->name_table
, NULL
);
1286 _mesa_hash_table_destroy(bufmgr
->handle_table
, NULL
);
1288 for (int z
= 0; z
< IRIS_MEMZONE_COUNT
; z
++) {
1289 util_vma_heap_finish(&bufmgr
->vma_allocator
[z
]);
1296 bo_set_tiling_internal(struct iris_bo
*bo
, uint32_t tiling_mode
,
1299 struct iris_bufmgr
*bufmgr
= bo
->bufmgr
;
1300 struct drm_i915_gem_set_tiling set_tiling
;
1303 if (bo
->global_name
== 0 &&
1304 tiling_mode
== bo
->tiling_mode
&& stride
== bo
->stride
)
1307 memset(&set_tiling
, 0, sizeof(set_tiling
));
1309 /* set_tiling is slightly broken and overwrites the
1310 * input on the error path, so we have to open code
1313 set_tiling
.handle
= bo
->gem_handle
;
1314 set_tiling
.tiling_mode
= tiling_mode
;
1315 set_tiling
.stride
= stride
;
1317 ret
= ioctl(bufmgr
->fd
, DRM_IOCTL_I915_GEM_SET_TILING
, &set_tiling
);
1318 } while (ret
== -1 && (errno
== EINTR
|| errno
== EAGAIN
));
1322 bo
->tiling_mode
= set_tiling
.tiling_mode
;
1323 bo
->swizzle_mode
= set_tiling
.swizzle_mode
;
1324 bo
->stride
= set_tiling
.stride
;
1329 iris_bo_get_tiling(struct iris_bo
*bo
, uint32_t *tiling_mode
,
1330 uint32_t *swizzle_mode
)
1332 *tiling_mode
= bo
->tiling_mode
;
1333 *swizzle_mode
= bo
->swizzle_mode
;
1338 iris_bo_import_dmabuf(struct iris_bufmgr
*bufmgr
, int prime_fd
)
1343 mtx_lock(&bufmgr
->lock
);
1344 int ret
= drmPrimeFDToHandle(bufmgr
->fd
, prime_fd
, &handle
);
1346 DBG("import_dmabuf: failed to obtain handle from fd: %s\n",
1348 mtx_unlock(&bufmgr
->lock
);
1353 * See if the kernel has already returned this buffer to us. Just as
1354 * for named buffers, we must not create two bo's pointing at the same
1357 bo
= hash_find_bo(bufmgr
->handle_table
, handle
);
1359 iris_bo_reference(bo
);
1367 p_atomic_set(&bo
->refcount
, 1);
1369 /* Determine size of bo. The fd-to-handle ioctl really should
1370 * return the size, but it doesn't. If we have kernel 3.12 or
1371 * later, we can lseek on the prime fd to get the size. Older
1372 * kernels will just fail, in which case we fall back to the
1373 * provided (estimated or guess size). */
1374 ret
= lseek(prime_fd
, 0, SEEK_END
);
1378 bo
->bufmgr
= bufmgr
;
1380 bo
->gem_handle
= handle
;
1381 _mesa_hash_table_insert(bufmgr
->handle_table
, &bo
->gem_handle
, bo
);
1384 bo
->reusable
= false;
1385 bo
->external
= true;
1386 bo
->kflags
= EXEC_OBJECT_SUPPORTS_48B_ADDRESS
| EXEC_OBJECT_PINNED
;
1387 bo
->gtt_offset
= vma_alloc(bufmgr
, IRIS_MEMZONE_OTHER
, bo
->size
, 1);
1389 struct drm_i915_gem_get_tiling get_tiling
= { .handle
= bo
->gem_handle
};
1390 if (drm_ioctl(bufmgr
->fd
, DRM_IOCTL_I915_GEM_GET_TILING
, &get_tiling
))
1393 bo
->tiling_mode
= get_tiling
.tiling_mode
;
1394 bo
->swizzle_mode
= get_tiling
.swizzle_mode
;
1395 /* XXX stride is unknown */
1398 mtx_unlock(&bufmgr
->lock
);
1403 mtx_unlock(&bufmgr
->lock
);
1408 iris_bo_make_external(struct iris_bo
*bo
)
1410 struct iris_bufmgr
*bufmgr
= bo
->bufmgr
;
1412 if (!bo
->external
) {
1413 mtx_lock(&bufmgr
->lock
);
1414 if (!bo
->external
) {
1415 _mesa_hash_table_insert(bufmgr
->handle_table
, &bo
->gem_handle
, bo
);
1416 bo
->external
= true;
1418 mtx_unlock(&bufmgr
->lock
);
1423 iris_bo_export_dmabuf(struct iris_bo
*bo
, int *prime_fd
)
1425 struct iris_bufmgr
*bufmgr
= bo
->bufmgr
;
1427 iris_bo_make_external(bo
);
1429 if (drmPrimeHandleToFD(bufmgr
->fd
, bo
->gem_handle
,
1430 DRM_CLOEXEC
, prime_fd
) != 0)
1433 bo
->reusable
= false;
1439 iris_bo_export_gem_handle(struct iris_bo
*bo
)
1441 iris_bo_make_external(bo
);
1443 return bo
->gem_handle
;
1447 iris_bo_flink(struct iris_bo
*bo
, uint32_t *name
)
1449 struct iris_bufmgr
*bufmgr
= bo
->bufmgr
;
1451 if (!bo
->global_name
) {
1452 struct drm_gem_flink flink
= { .handle
= bo
->gem_handle
};
1454 if (drm_ioctl(bufmgr
->fd
, DRM_IOCTL_GEM_FLINK
, &flink
))
1457 iris_bo_make_external(bo
);
1458 mtx_lock(&bufmgr
->lock
);
1459 if (!bo
->global_name
) {
1460 bo
->global_name
= flink
.name
;
1461 _mesa_hash_table_insert(bufmgr
->name_table
, &bo
->global_name
, bo
);
1463 mtx_unlock(&bufmgr
->lock
);
1465 bo
->reusable
= false;
1468 *name
= bo
->global_name
;
1473 add_bucket(struct iris_bufmgr
*bufmgr
, int size
)
1475 unsigned int i
= bufmgr
->num_buckets
;
1477 assert(i
< ARRAY_SIZE(bufmgr
->cache_bucket
));
1479 list_inithead(&bufmgr
->cache_bucket
[i
].head
);
1480 for (int z
= 0; z
< IRIS_MEMZONE_COUNT
; z
++)
1481 util_dynarray_init(&bufmgr
->cache_bucket
[i
].vma_list
[z
], NULL
);
1482 bufmgr
->cache_bucket
[i
].size
= size
;
1483 bufmgr
->num_buckets
++;
1485 assert(bucket_for_size(bufmgr
, size
) == &bufmgr
->cache_bucket
[i
]);
1486 assert(bucket_for_size(bufmgr
, size
- 2048) == &bufmgr
->cache_bucket
[i
]);
1487 assert(bucket_for_size(bufmgr
, size
+ 1) != &bufmgr
->cache_bucket
[i
]);
1491 init_cache_buckets(struct iris_bufmgr
*bufmgr
)
1493 uint64_t size
, cache_max_size
= 64 * 1024 * 1024;
1495 /* OK, so power of two buckets was too wasteful of memory.
1496 * Give 3 other sizes between each power of two, to hopefully
1497 * cover things accurately enough. (The alternative is
1498 * probably to just go for exact matching of sizes, and assume
1499 * that for things like composited window resize the tiled
1500 * width/height alignment and rounding of sizes to pages will
1501 * get us useful cache hit rates anyway)
1503 add_bucket(bufmgr
, PAGE_SIZE
);
1504 add_bucket(bufmgr
, PAGE_SIZE
* 2);
1505 add_bucket(bufmgr
, PAGE_SIZE
* 3);
1507 /* Initialize the linked lists for BO reuse cache. */
1508 for (size
= 4 * PAGE_SIZE
; size
<= cache_max_size
; size
*= 2) {
1509 add_bucket(bufmgr
, size
);
1511 add_bucket(bufmgr
, size
+ size
* 1 / 4);
1512 add_bucket(bufmgr
, size
+ size
* 2 / 4);
1513 add_bucket(bufmgr
, size
+ size
* 3 / 4);
1518 iris_create_hw_context(struct iris_bufmgr
*bufmgr
)
1520 struct drm_i915_gem_context_create create
= { };
1521 int ret
= drm_ioctl(bufmgr
->fd
, DRM_IOCTL_I915_GEM_CONTEXT_CREATE
, &create
);
1523 DBG("DRM_IOCTL_I915_GEM_CONTEXT_CREATE failed: %s\n", strerror(errno
));
1527 return create
.ctx_id
;
1531 iris_hw_context_set_priority(struct iris_bufmgr
*bufmgr
,
1535 struct drm_i915_gem_context_param p
= {
1537 .param
= I915_CONTEXT_PARAM_PRIORITY
,
1543 if (drm_ioctl(bufmgr
->fd
, DRM_IOCTL_I915_GEM_CONTEXT_SETPARAM
, &p
))
1550 iris_destroy_hw_context(struct iris_bufmgr
*bufmgr
, uint32_t ctx_id
)
1552 struct drm_i915_gem_context_destroy d
= { .ctx_id
= ctx_id
};
1555 drm_ioctl(bufmgr
->fd
, DRM_IOCTL_I915_GEM_CONTEXT_DESTROY
, &d
) != 0) {
1556 fprintf(stderr
, "DRM_IOCTL_I915_GEM_CONTEXT_DESTROY failed: %s\n",
1562 iris_reg_read(struct iris_bufmgr
*bufmgr
, uint32_t offset
, uint64_t *result
)
1564 struct drm_i915_reg_read reg_read
= { .offset
= offset
};
1565 int ret
= drm_ioctl(bufmgr
->fd
, DRM_IOCTL_I915_REG_READ
, ®_read
);
1567 *result
= reg_read
.val
;
1572 * Initializes the GEM buffer manager, which uses the kernel to allocate, map,
1573 * and manage map buffer objections.
1575 * \param fd File descriptor of the opened DRM device.
1577 struct iris_bufmgr
*
1578 iris_bufmgr_init(struct gen_device_info
*devinfo
, int fd
)
1580 struct iris_bufmgr
*bufmgr
= calloc(1, sizeof(*bufmgr
));
1584 /* Handles to buffer objects belong to the device fd and are not
1585 * reference counted by the kernel. If the same fd is used by
1586 * multiple parties (threads sharing the same screen bufmgr, or
1587 * even worse the same device fd passed to multiple libraries)
1588 * ownership of those handles is shared by those independent parties.
1590 * Don't do this! Ensure that each library/bufmgr has its own device
1591 * fd so that its namespace does not clash with another.
1595 if (mtx_init(&bufmgr
->lock
, mtx_plain
) != 0) {
1600 bufmgr
->has_llc
= devinfo
->has_llc
;
1602 STATIC_ASSERT(IRIS_MEMZONE_SHADER_START
== 0ull);
1603 const uint64_t _4GB
= 1ull << 32;
1605 util_vma_heap_init(&bufmgr
->vma_allocator
[IRIS_MEMZONE_SHADER
],
1607 util_vma_heap_init(&bufmgr
->vma_allocator
[IRIS_MEMZONE_BINDER
],
1608 IRIS_MEMZONE_BINDER_START
,
1609 IRIS_MAX_BINDERS
* IRIS_BINDER_SIZE
);
1610 util_vma_heap_init(&bufmgr
->vma_allocator
[IRIS_MEMZONE_SURFACE
],
1611 IRIS_MEMZONE_SURFACE_START
,
1612 _4GB
- IRIS_MAX_BINDERS
* IRIS_BINDER_SIZE
);
1613 util_vma_heap_init(&bufmgr
->vma_allocator
[IRIS_MEMZONE_DYNAMIC
],
1614 IRIS_MEMZONE_DYNAMIC_START
+ IRIS_BORDER_COLOR_POOL_SIZE
,
1615 _4GB
- IRIS_BORDER_COLOR_POOL_SIZE
);
1616 util_vma_heap_init(&bufmgr
->vma_allocator
[IRIS_MEMZONE_OTHER
],
1617 IRIS_MEMZONE_OTHER_START
,
1618 (1ull << 48) - IRIS_MEMZONE_OTHER_START
);
1621 bufmgr
->bo_reuse
= env_var_as_boolean("bo_reuse", true);
1623 init_cache_buckets(bufmgr
);
1625 bufmgr
->name_table
=
1626 _mesa_hash_table_create(NULL
, key_hash_uint
, key_uint_equal
);
1627 bufmgr
->handle_table
=
1628 _mesa_hash_table_create(NULL
, key_hash_uint
, key_uint_equal
);