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/list.h"
65 #include "brw_bufmgr.h"
79 #define memclear(s) memset(&s, 0, sizeof(s))
81 #define FILE_DEBUG_FLAG DEBUG_BUFMGR
84 atomic_add_unless(int *v
, int add
, int unless
)
88 while (c
!= unless
&& (old
= p_atomic_cmpxchg(v
, c
, c
+ add
)) != c
)
94 * upper_32_bits - return bits 32-63 of a number
95 * @n: the number we're accessing
97 * A basic shift-right of a 64- or 32-bit quantity. Use this to suppress
98 * the "right shift count >= width of type" warning when that quantity is
101 #define upper_32_bits(n) ((__u32)(((n) >> 16) >> 16))
104 * lower_32_bits - return bits 0-31 of a number
105 * @n: the number we're accessing
107 #define lower_32_bits(n) ((__u32)(n))
109 struct _drm_bacon_context
{
111 struct _drm_bacon_bufmgr
*bufmgr
;
114 typedef struct _drm_bacon_bo_gem drm_bacon_bo_gem
;
116 struct drm_bacon_gem_bo_bucket
{
117 struct list_head head
;
121 typedef struct _drm_bacon_bufmgr
{
128 pthread_mutex_t lock
;
130 struct drm_i915_gem_exec_object2
*exec2_objects
;
131 drm_bacon_bo
**exec_bos
;
135 /** Array of lists of cached gem objects of power-of-two sizes */
136 struct drm_bacon_gem_bo_bucket cache_bucket
[14 * 4];
140 struct list_head managers
;
142 drm_bacon_bo_gem
*name_table
;
143 drm_bacon_bo_gem
*handle_table
;
145 struct list_head vma_cache
;
146 int vma_count
, vma_open
, vma_max
;
149 unsigned int has_llc
: 1;
150 unsigned int bo_reuse
: 1;
151 unsigned int no_exec
: 1;
152 unsigned int has_exec_async
: 1;
161 typedef struct _drm_bacon_reloc_target_info
{
163 } drm_bacon_reloc_target
;
165 struct _drm_bacon_bo_gem
{
173 * Kenel-assigned global name for this object
175 * List contains both flink named and prime fd'd objects
177 unsigned int global_name
;
179 UT_hash_handle handle_hh
;
180 UT_hash_handle name_hh
;
183 * Index of the buffer within the validation list while preparing a
184 * batchbuffer execution.
189 * Current tiling mode
191 uint32_t tiling_mode
;
192 uint32_t swizzle_mode
;
193 unsigned long stride
;
195 unsigned long kflags
;
199 /** Array passed to the DRM containing relocation information. */
200 struct drm_i915_gem_relocation_entry
*relocs
;
202 * Array of info structs corresponding to relocs[i].target_handle etc
204 drm_bacon_reloc_target
*reloc_target_info
;
205 /** Number of entries in relocs */
207 /** Array of BOs that are referenced by this buffer and will be softpinned */
208 drm_bacon_bo
**softpin_target
;
209 /** Number softpinned BOs that are referenced by this buffer */
210 int softpin_target_count
;
211 /** Maximum amount of softpinned BOs that are referenced by this buffer */
212 int softpin_target_size
;
214 /** Mapped address for the buffer, saved across map/unmap cycles */
216 /** GTT virtual address for the buffer, saved across map/unmap cycles */
218 /** WC CPU address for the buffer, saved across map/unmap cycles */
221 * Virtual address of the buffer allocated by user, used for userptr
226 struct list_head vma_list
;
229 struct list_head head
;
232 * Boolean of whether this BO and its children have been included in
233 * the current drm_bacon_bufmgr_check_aperture_space() total.
235 bool included_in_check_aperture
;
238 * Boolean of whether this buffer has been used as a relocation
239 * target and had its size accounted for, and thus can't have any
240 * further relocations added to it.
242 bool used_as_reloc_target
;
245 * Boolean of whether we have encountered an error whilst building the relocation tree.
250 * Boolean of whether this buffer can be re-used
255 * Boolean of whether the GPU is definitely not accessing the buffer.
257 * This is only valid when reusable, since non-reusable
258 * buffers are those that have been shared with other
259 * processes, so we don't know their state.
264 * Boolean of whether this buffer was allocated with userptr
269 * Size in bytes of this buffer and its relocation descendents.
271 * Used to avoid costly tree walking in
272 * drm_bacon_bufmgr_check_aperture in the common case.
276 /** Flags that we may need to do the SW_FINISH ioctl on unmap. */
277 bool mapped_cpu_write
;
281 drm_bacon_gem_estimate_batch_space(drm_bacon_bo
** bo_array
, int count
);
284 drm_bacon_gem_compute_batch_space(drm_bacon_bo
** bo_array
, int count
);
287 drm_bacon_gem_bo_set_tiling_internal(drm_bacon_bo
*bo
,
288 uint32_t tiling_mode
,
291 static void drm_bacon_gem_bo_unreference_locked_timed(drm_bacon_bo
*bo
,
294 static void drm_bacon_gem_bo_free(drm_bacon_bo
*bo
);
296 static inline drm_bacon_bo_gem
*to_bo_gem(drm_bacon_bo
*bo
)
298 return (drm_bacon_bo_gem
*)bo
;
302 drm_bacon_gem_bo_tile_size(drm_bacon_bufmgr
*bufmgr
, unsigned long size
,
303 uint32_t *tiling_mode
)
305 if (*tiling_mode
== I915_TILING_NONE
)
308 /* 965+ just need multiples of page size for tiling */
309 return ALIGN(size
, 4096);
313 * Round a given pitch up to the minimum required for X tiling on a
314 * given chip. We use 512 as the minimum to allow for a later tiling
318 drm_bacon_gem_bo_tile_pitch(drm_bacon_bufmgr
*bufmgr
,
319 unsigned long pitch
, uint32_t *tiling_mode
)
321 unsigned long tile_width
;
323 /* If untiled, then just align it so that we can do rendering
324 * to it with the 3D engine.
326 if (*tiling_mode
== I915_TILING_NONE
)
327 return ALIGN(pitch
, 64);
329 if (*tiling_mode
== I915_TILING_X
)
334 /* 965 is flexible */
335 return ALIGN(pitch
, tile_width
);
338 static struct drm_bacon_gem_bo_bucket
*
339 drm_bacon_gem_bo_bucket_for_size(drm_bacon_bufmgr
*bufmgr
,
344 for (i
= 0; i
< bufmgr
->num_buckets
; i
++) {
345 struct drm_bacon_gem_bo_bucket
*bucket
=
346 &bufmgr
->cache_bucket
[i
];
347 if (bucket
->size
>= size
) {
356 drm_bacon_gem_dump_validation_list(drm_bacon_bufmgr
*bufmgr
)
360 for (i
= 0; i
< bufmgr
->exec_count
; i
++) {
361 drm_bacon_bo
*bo
= bufmgr
->exec_bos
[i
];
362 drm_bacon_bo_gem
*bo_gem
= (drm_bacon_bo_gem
*) bo
;
364 if (bo_gem
->relocs
== NULL
&& bo_gem
->softpin_target
== NULL
) {
365 DBG("%2d: %d %s(%s)\n", i
, bo_gem
->gem_handle
,
366 bo_gem
->kflags
& EXEC_OBJECT_PINNED
? "*" : "",
371 for (j
= 0; j
< bo_gem
->reloc_count
; j
++) {
372 drm_bacon_bo
*target_bo
= bo_gem
->reloc_target_info
[j
].bo
;
373 drm_bacon_bo_gem
*target_gem
=
374 (drm_bacon_bo_gem
*) target_bo
;
376 DBG("%2d: %d %s(%s)@0x%08x %08x -> "
377 "%d (%s)@0x%08x %08x + 0x%08x\n",
380 bo_gem
->kflags
& EXEC_OBJECT_PINNED
? "*" : "",
382 upper_32_bits(bo_gem
->relocs
[j
].offset
),
383 lower_32_bits(bo_gem
->relocs
[j
].offset
),
384 target_gem
->gem_handle
,
386 upper_32_bits(target_bo
->offset64
),
387 lower_32_bits(target_bo
->offset64
),
388 bo_gem
->relocs
[j
].delta
);
391 for (j
= 0; j
< bo_gem
->softpin_target_count
; j
++) {
392 drm_bacon_bo
*target_bo
= bo_gem
->softpin_target
[j
];
393 drm_bacon_bo_gem
*target_gem
=
394 (drm_bacon_bo_gem
*) target_bo
;
395 DBG("%2d: %d %s(%s) -> "
396 "%d *(%s)@0x%08x %08x\n",
399 bo_gem
->kflags
& EXEC_OBJECT_PINNED
? "*" : "",
401 target_gem
->gem_handle
,
403 upper_32_bits(target_bo
->offset64
),
404 lower_32_bits(target_bo
->offset64
));
410 drm_bacon_bo_reference(drm_bacon_bo
*bo
)
412 drm_bacon_bo_gem
*bo_gem
= (drm_bacon_bo_gem
*) bo
;
414 p_atomic_inc(&bo_gem
->refcount
);
418 drm_bacon_add_validate_buffer2(drm_bacon_bo
*bo
)
420 drm_bacon_bufmgr
*bufmgr
= bo
->bufmgr
;
421 drm_bacon_bo_gem
*bo_gem
= (drm_bacon_bo_gem
*)bo
;
424 if (bo_gem
->validate_index
!= -1)
427 /* Extend the array of validation entries as necessary. */
428 if (bufmgr
->exec_count
== bufmgr
->exec_size
) {
429 int new_size
= bufmgr
->exec_size
* 2;
434 bufmgr
->exec2_objects
=
435 realloc(bufmgr
->exec2_objects
,
436 sizeof(*bufmgr
->exec2_objects
) * new_size
);
438 realloc(bufmgr
->exec_bos
,
439 sizeof(*bufmgr
->exec_bos
) * new_size
);
440 bufmgr
->exec_size
= new_size
;
443 index
= bufmgr
->exec_count
;
444 bo_gem
->validate_index
= index
;
445 /* Fill in array entry */
446 bufmgr
->exec2_objects
[index
].handle
= bo_gem
->gem_handle
;
447 bufmgr
->exec2_objects
[index
].relocation_count
= bo_gem
->reloc_count
;
448 bufmgr
->exec2_objects
[index
].relocs_ptr
= (uintptr_t)bo_gem
->relocs
;
449 bufmgr
->exec2_objects
[index
].alignment
= bo
->align
;
450 bufmgr
->exec2_objects
[index
].offset
= bo
->offset64
;
451 bufmgr
->exec2_objects
[index
].flags
= bo_gem
->kflags
;
452 bufmgr
->exec2_objects
[index
].rsvd1
= 0;
453 bufmgr
->exec2_objects
[index
].rsvd2
= 0;
454 bufmgr
->exec_bos
[index
] = bo
;
455 bufmgr
->exec_count
++;
459 drm_bacon_bo_gem_set_in_aperture_size(drm_bacon_bufmgr
*bufmgr
,
460 drm_bacon_bo_gem
*bo_gem
,
461 unsigned int alignment
)
465 assert(!bo_gem
->used_as_reloc_target
);
467 /* The older chipsets are far-less flexible in terms of tiling,
468 * and require tiled buffer to be size aligned in the aperture.
469 * This means that in the worst possible case we will need a hole
470 * twice as large as the object in order for it to fit into the
471 * aperture. Optimal packing is for wimps.
473 size
= bo_gem
->bo
.size
;
475 bo_gem
->reloc_tree_size
= size
+ alignment
;
479 drm_bacon_setup_reloc_list(drm_bacon_bo
*bo
)
481 drm_bacon_bo_gem
*bo_gem
= (drm_bacon_bo_gem
*) bo
;
482 drm_bacon_bufmgr
*bufmgr
= bo
->bufmgr
;
483 unsigned int max_relocs
= bufmgr
->max_relocs
;
485 if (bo
->size
/ 4 < max_relocs
)
486 max_relocs
= bo
->size
/ 4;
488 bo_gem
->relocs
= malloc(max_relocs
*
489 sizeof(struct drm_i915_gem_relocation_entry
));
490 bo_gem
->reloc_target_info
= malloc(max_relocs
*
491 sizeof(drm_bacon_reloc_target
));
492 if (bo_gem
->relocs
== NULL
|| bo_gem
->reloc_target_info
== NULL
) {
493 bo_gem
->has_error
= true;
495 free (bo_gem
->relocs
);
496 bo_gem
->relocs
= NULL
;
498 free (bo_gem
->reloc_target_info
);
499 bo_gem
->reloc_target_info
= NULL
;
508 drm_bacon_bo_busy(drm_bacon_bo
*bo
)
510 drm_bacon_bufmgr
*bufmgr
= bo
->bufmgr
;
511 drm_bacon_bo_gem
*bo_gem
= (drm_bacon_bo_gem
*) bo
;
512 struct drm_i915_gem_busy busy
;
515 if (bo_gem
->reusable
&& bo_gem
->idle
)
519 busy
.handle
= bo_gem
->gem_handle
;
521 ret
= drmIoctl(bufmgr
->fd
, DRM_IOCTL_I915_GEM_BUSY
, &busy
);
523 bo_gem
->idle
= !busy
.busy
;
528 return (ret
== 0 && busy
.busy
);
532 drm_bacon_gem_bo_madvise_internal(drm_bacon_bufmgr
*bufmgr
,
533 drm_bacon_bo_gem
*bo_gem
, int state
)
535 struct drm_i915_gem_madvise madv
;
538 madv
.handle
= bo_gem
->gem_handle
;
541 drmIoctl(bufmgr
->fd
, DRM_IOCTL_I915_GEM_MADVISE
, &madv
);
543 return madv
.retained
;
547 drm_bacon_bo_madvise(drm_bacon_bo
*bo
, int madv
)
549 return drm_bacon_gem_bo_madvise_internal(bo
->bufmgr
,
550 (drm_bacon_bo_gem
*) bo
,
554 /* drop the oldest entries that have been purged by the kernel */
556 drm_bacon_gem_bo_cache_purge_bucket(drm_bacon_bufmgr
*bufmgr
,
557 struct drm_bacon_gem_bo_bucket
*bucket
)
559 while (!list_empty(&bucket
->head
)) {
560 drm_bacon_bo_gem
*bo_gem
;
562 bo_gem
= LIST_ENTRY(drm_bacon_bo_gem
,
563 bucket
->head
.next
, head
);
564 if (drm_bacon_gem_bo_madvise_internal
565 (bufmgr
, bo_gem
, I915_MADV_DONTNEED
))
568 list_del(&bo_gem
->head
);
569 drm_bacon_gem_bo_free(&bo_gem
->bo
);
573 static drm_bacon_bo
*
574 drm_bacon_gem_bo_alloc_internal(drm_bacon_bufmgr
*bufmgr
,
578 uint32_t tiling_mode
,
579 unsigned long stride
,
580 unsigned int alignment
)
582 drm_bacon_bo_gem
*bo_gem
;
583 unsigned int page_size
= getpagesize();
585 struct drm_bacon_gem_bo_bucket
*bucket
;
586 bool alloc_from_cache
;
587 unsigned long bo_size
;
588 bool for_render
= false;
590 if (flags
& BO_ALLOC_FOR_RENDER
)
593 /* Round the allocated size up to a power of two number of pages. */
594 bucket
= drm_bacon_gem_bo_bucket_for_size(bufmgr
, size
);
596 /* If we don't have caching at this size, don't actually round the
599 if (bucket
== NULL
) {
601 if (bo_size
< page_size
)
604 bo_size
= bucket
->size
;
607 pthread_mutex_lock(&bufmgr
->lock
);
608 /* Get a buffer out of the cache if available */
610 alloc_from_cache
= false;
611 if (bucket
!= NULL
&& !list_empty(&bucket
->head
)) {
613 /* Allocate new render-target BOs from the tail (MRU)
614 * of the list, as it will likely be hot in the GPU
615 * cache and in the aperture for us.
617 bo_gem
= LIST_ENTRY(drm_bacon_bo_gem
,
618 bucket
->head
.prev
, head
);
619 list_del(&bo_gem
->head
);
620 alloc_from_cache
= true;
621 bo_gem
->bo
.align
= alignment
;
623 assert(alignment
== 0);
624 /* For non-render-target BOs (where we're probably
625 * going to map it first thing in order to fill it
626 * with data), check if the last BO in the cache is
627 * unbusy, and only reuse in that case. Otherwise,
628 * allocating a new buffer is probably faster than
629 * waiting for the GPU to finish.
631 bo_gem
= LIST_ENTRY(drm_bacon_bo_gem
,
632 bucket
->head
.next
, head
);
633 if (!drm_bacon_bo_busy(&bo_gem
->bo
)) {
634 alloc_from_cache
= true;
635 list_del(&bo_gem
->head
);
639 if (alloc_from_cache
) {
640 if (!drm_bacon_gem_bo_madvise_internal
641 (bufmgr
, bo_gem
, I915_MADV_WILLNEED
)) {
642 drm_bacon_gem_bo_free(&bo_gem
->bo
);
643 drm_bacon_gem_bo_cache_purge_bucket(bufmgr
,
648 if (drm_bacon_gem_bo_set_tiling_internal(&bo_gem
->bo
,
651 drm_bacon_gem_bo_free(&bo_gem
->bo
);
657 if (!alloc_from_cache
) {
658 struct drm_i915_gem_create create
;
660 bo_gem
= calloc(1, sizeof(*bo_gem
));
664 /* drm_bacon_gem_bo_free calls list_del() for an uninitialized
665 list (vma_list), so better set the list head here */
666 list_inithead(&bo_gem
->vma_list
);
668 bo_gem
->bo
.size
= bo_size
;
671 create
.size
= bo_size
;
673 ret
= drmIoctl(bufmgr
->fd
,
674 DRM_IOCTL_I915_GEM_CREATE
,
681 bo_gem
->gem_handle
= create
.handle
;
682 HASH_ADD(handle_hh
, bufmgr
->handle_table
,
683 gem_handle
, sizeof(bo_gem
->gem_handle
),
686 bo_gem
->bo
.handle
= bo_gem
->gem_handle
;
687 bo_gem
->bo
.bufmgr
= bufmgr
;
688 bo_gem
->bo
.align
= alignment
;
690 bo_gem
->tiling_mode
= I915_TILING_NONE
;
691 bo_gem
->swizzle_mode
= I915_BIT_6_SWIZZLE_NONE
;
694 if (drm_bacon_gem_bo_set_tiling_internal(&bo_gem
->bo
,
701 p_atomic_set(&bo_gem
->refcount
, 1);
702 bo_gem
->validate_index
= -1;
703 bo_gem
->used_as_reloc_target
= false;
704 bo_gem
->has_error
= false;
705 bo_gem
->reusable
= true;
707 drm_bacon_bo_gem_set_in_aperture_size(bufmgr
, bo_gem
, alignment
);
708 pthread_mutex_unlock(&bufmgr
->lock
);
710 DBG("bo_create: buf %d (%s) %ldb\n",
711 bo_gem
->gem_handle
, bo_gem
->name
, size
);
716 drm_bacon_gem_bo_free(&bo_gem
->bo
);
718 pthread_mutex_unlock(&bufmgr
->lock
);
723 drm_bacon_bo_alloc_for_render(drm_bacon_bufmgr
*bufmgr
,
726 unsigned int alignment
)
728 return drm_bacon_gem_bo_alloc_internal(bufmgr
, name
, size
,
735 drm_bacon_bo_alloc(drm_bacon_bufmgr
*bufmgr
,
738 unsigned int alignment
)
740 return drm_bacon_gem_bo_alloc_internal(bufmgr
, name
, size
, 0,
741 I915_TILING_NONE
, 0, 0);
745 drm_bacon_bo_alloc_tiled(drm_bacon_bufmgr
*bufmgr
, const char *name
,
746 int x
, int y
, int cpp
, uint32_t *tiling_mode
,
747 unsigned long *pitch
, unsigned long flags
)
749 unsigned long size
, stride
;
753 unsigned long aligned_y
, height_alignment
;
755 tiling
= *tiling_mode
;
757 /* If we're tiled, our allocations are in 8 or 32-row blocks,
758 * so failure to align our height means that we won't allocate
761 * If we're untiled, we still have to align to 2 rows high
762 * because the data port accesses 2x2 blocks even if the
763 * bottom row isn't to be rendered, so failure to align means
764 * we could walk off the end of the GTT and fault. This is
765 * documented on 965, and may be the case on older chipsets
766 * too so we try to be careful.
769 height_alignment
= 2;
771 if (tiling
== I915_TILING_X
)
772 height_alignment
= 8;
773 else if (tiling
== I915_TILING_Y
)
774 height_alignment
= 32;
775 aligned_y
= ALIGN(y
, height_alignment
);
778 stride
= drm_bacon_gem_bo_tile_pitch(bufmgr
, stride
, tiling_mode
);
779 size
= stride
* aligned_y
;
780 size
= drm_bacon_gem_bo_tile_size(bufmgr
, size
, tiling_mode
);
781 } while (*tiling_mode
!= tiling
);
784 if (tiling
== I915_TILING_NONE
)
787 return drm_bacon_gem_bo_alloc_internal(bufmgr
, name
, size
, flags
,
792 drm_bacon_bo_alloc_userptr(drm_bacon_bufmgr
*bufmgr
,
795 uint32_t tiling_mode
,
800 drm_bacon_bo_gem
*bo_gem
;
802 struct drm_i915_gem_userptr userptr
;
804 /* Tiling with userptr surfaces is not supported
805 * on all hardware so refuse it for time being.
807 if (tiling_mode
!= I915_TILING_NONE
)
810 bo_gem
= calloc(1, sizeof(*bo_gem
));
814 p_atomic_set(&bo_gem
->refcount
, 1);
815 list_inithead(&bo_gem
->vma_list
);
817 bo_gem
->bo
.size
= size
;
820 userptr
.user_ptr
= (__u64
)((unsigned long)addr
);
821 userptr
.user_size
= size
;
822 userptr
.flags
= flags
;
824 ret
= drmIoctl(bufmgr
->fd
,
825 DRM_IOCTL_I915_GEM_USERPTR
,
828 DBG("bo_create_userptr: "
829 "ioctl failed with user ptr %p size 0x%lx, "
830 "user flags 0x%lx\n", addr
, size
, flags
);
835 pthread_mutex_lock(&bufmgr
->lock
);
837 bo_gem
->gem_handle
= userptr
.handle
;
838 bo_gem
->bo
.handle
= bo_gem
->gem_handle
;
839 bo_gem
->bo
.bufmgr
= bufmgr
;
840 bo_gem
->is_userptr
= true;
841 bo_gem
->bo
.virtual = addr
;
842 /* Save the address provided by user */
843 bo_gem
->user_virtual
= addr
;
844 bo_gem
->tiling_mode
= I915_TILING_NONE
;
845 bo_gem
->swizzle_mode
= I915_BIT_6_SWIZZLE_NONE
;
848 HASH_ADD(handle_hh
, bufmgr
->handle_table
,
849 gem_handle
, sizeof(bo_gem
->gem_handle
),
853 bo_gem
->validate_index
= -1;
854 bo_gem
->used_as_reloc_target
= false;
855 bo_gem
->has_error
= false;
856 bo_gem
->reusable
= false;
858 drm_bacon_bo_gem_set_in_aperture_size(bufmgr
, bo_gem
, 0);
859 pthread_mutex_unlock(&bufmgr
->lock
);
861 DBG("bo_create_userptr: "
862 "ptr %p buf %d (%s) size %ldb, stride 0x%x, tile mode %d\n",
863 addr
, bo_gem
->gem_handle
, bo_gem
->name
,
864 size
, stride
, tiling_mode
);
870 drm_bacon_has_userptr(drm_bacon_bufmgr
*bufmgr
)
875 struct drm_i915_gem_userptr userptr
;
877 pgsz
= sysconf(_SC_PAGESIZE
);
880 ret
= posix_memalign(&ptr
, pgsz
, pgsz
);
882 DBG("Failed to get a page (%ld) for userptr detection!\n",
888 userptr
.user_ptr
= (__u64
)(unsigned long)ptr
;
889 userptr
.user_size
= pgsz
;
892 ret
= drmIoctl(bufmgr
->fd
, DRM_IOCTL_I915_GEM_USERPTR
, &userptr
);
894 if (errno
== ENODEV
&& userptr
.flags
== 0) {
895 userptr
.flags
= I915_USERPTR_UNSYNCHRONIZED
;
902 /* We don't release the userptr bo here as we want to keep the
903 * kernel mm tracking alive for our lifetime. The first time we
904 * create a userptr object the kernel has to install a mmu_notifer
905 * which is a heavyweight operation (e.g. it requires taking all
906 * mm_locks and stop_machine()).
909 bufmgr
->userptr_active
.ptr
= ptr
;
910 bufmgr
->userptr_active
.handle
= userptr
.handle
;
916 * Returns a drm_bacon_bo wrapping the given buffer object handle.
918 * This can be used when one application needs to pass a buffer object
922 drm_bacon_bo_gem_create_from_name(drm_bacon_bufmgr
*bufmgr
,
926 drm_bacon_bo_gem
*bo_gem
;
928 struct drm_gem_open open_arg
;
929 struct drm_i915_gem_get_tiling get_tiling
;
931 /* At the moment most applications only have a few named bo.
932 * For instance, in a DRI client only the render buffers passed
933 * between X and the client are named. And since X returns the
934 * alternating names for the front/back buffer a linear search
935 * provides a sufficiently fast match.
937 pthread_mutex_lock(&bufmgr
->lock
);
938 HASH_FIND(name_hh
, bufmgr
->name_table
,
939 &handle
, sizeof(handle
), bo_gem
);
941 drm_bacon_bo_reference(&bo_gem
->bo
);
946 open_arg
.name
= handle
;
947 ret
= drmIoctl(bufmgr
->fd
,
951 DBG("Couldn't reference %s handle 0x%08x: %s\n",
952 name
, handle
, strerror(errno
));
956 /* Now see if someone has used a prime handle to get this
957 * object from the kernel before by looking through the list
958 * again for a matching gem_handle
960 HASH_FIND(handle_hh
, bufmgr
->handle_table
,
961 &open_arg
.handle
, sizeof(open_arg
.handle
), bo_gem
);
963 drm_bacon_bo_reference(&bo_gem
->bo
);
967 bo_gem
= calloc(1, sizeof(*bo_gem
));
971 p_atomic_set(&bo_gem
->refcount
, 1);
972 list_inithead(&bo_gem
->vma_list
);
974 bo_gem
->bo
.size
= open_arg
.size
;
975 bo_gem
->bo
.offset64
= 0;
976 bo_gem
->bo
.virtual = NULL
;
977 bo_gem
->bo
.bufmgr
= bufmgr
;
979 bo_gem
->validate_index
= -1;
980 bo_gem
->gem_handle
= open_arg
.handle
;
981 bo_gem
->bo
.handle
= open_arg
.handle
;
982 bo_gem
->global_name
= handle
;
983 bo_gem
->reusable
= false;
985 HASH_ADD(handle_hh
, bufmgr
->handle_table
,
986 gem_handle
, sizeof(bo_gem
->gem_handle
), bo_gem
);
987 HASH_ADD(name_hh
, bufmgr
->name_table
,
988 global_name
, sizeof(bo_gem
->global_name
), bo_gem
);
990 memclear(get_tiling
);
991 get_tiling
.handle
= bo_gem
->gem_handle
;
992 ret
= drmIoctl(bufmgr
->fd
,
993 DRM_IOCTL_I915_GEM_GET_TILING
,
998 bo_gem
->tiling_mode
= get_tiling
.tiling_mode
;
999 bo_gem
->swizzle_mode
= get_tiling
.swizzle_mode
;
1000 /* XXX stride is unknown */
1001 drm_bacon_bo_gem_set_in_aperture_size(bufmgr
, bo_gem
, 0);
1002 DBG("bo_create_from_handle: %d (%s)\n", handle
, bo_gem
->name
);
1005 pthread_mutex_unlock(&bufmgr
->lock
);
1009 drm_bacon_gem_bo_free(&bo_gem
->bo
);
1010 pthread_mutex_unlock(&bufmgr
->lock
);
1015 drm_bacon_gem_bo_free(drm_bacon_bo
*bo
)
1017 drm_bacon_bufmgr
*bufmgr
= bo
->bufmgr
;
1018 drm_bacon_bo_gem
*bo_gem
= (drm_bacon_bo_gem
*) bo
;
1019 struct drm_gem_close close
;
1022 list_del(&bo_gem
->vma_list
);
1023 if (bo_gem
->mem_virtual
) {
1024 VG(VALGRIND_FREELIKE_BLOCK(bo_gem
->mem_virtual
, 0));
1025 drm_munmap(bo_gem
->mem_virtual
, bo_gem
->bo
.size
);
1026 bufmgr
->vma_count
--;
1028 if (bo_gem
->wc_virtual
) {
1029 VG(VALGRIND_FREELIKE_BLOCK(bo_gem
->wc_virtual
, 0));
1030 drm_munmap(bo_gem
->wc_virtual
, bo_gem
->bo
.size
);
1031 bufmgr
->vma_count
--;
1033 if (bo_gem
->gtt_virtual
) {
1034 drm_munmap(bo_gem
->gtt_virtual
, bo_gem
->bo
.size
);
1035 bufmgr
->vma_count
--;
1038 if (bo_gem
->global_name
)
1039 HASH_DELETE(name_hh
, bufmgr
->name_table
, bo_gem
);
1040 HASH_DELETE(handle_hh
, bufmgr
->handle_table
, bo_gem
);
1042 /* Close this object */
1044 close
.handle
= bo_gem
->gem_handle
;
1045 ret
= drmIoctl(bufmgr
->fd
, DRM_IOCTL_GEM_CLOSE
, &close
);
1047 DBG("DRM_IOCTL_GEM_CLOSE %d failed (%s): %s\n",
1048 bo_gem
->gem_handle
, bo_gem
->name
, strerror(errno
));
1054 drm_bacon_gem_bo_mark_mmaps_incoherent(drm_bacon_bo
*bo
)
1057 drm_bacon_bo_gem
*bo_gem
= (drm_bacon_bo_gem
*) bo
;
1059 if (bo_gem
->mem_virtual
)
1060 VALGRIND_MAKE_MEM_NOACCESS(bo_gem
->mem_virtual
, bo
->size
);
1062 if (bo_gem
->wc_virtual
)
1063 VALGRIND_MAKE_MEM_NOACCESS(bo_gem
->wc_virtual
, bo
->size
);
1065 if (bo_gem
->gtt_virtual
)
1066 VALGRIND_MAKE_MEM_NOACCESS(bo_gem
->gtt_virtual
, bo
->size
);
1070 /** Frees all cached buffers significantly older than @time. */
1072 drm_bacon_gem_cleanup_bo_cache(drm_bacon_bufmgr
*bufmgr
, time_t time
)
1076 if (bufmgr
->time
== time
)
1079 for (i
= 0; i
< bufmgr
->num_buckets
; i
++) {
1080 struct drm_bacon_gem_bo_bucket
*bucket
=
1081 &bufmgr
->cache_bucket
[i
];
1083 while (!list_empty(&bucket
->head
)) {
1084 drm_bacon_bo_gem
*bo_gem
;
1086 bo_gem
= LIST_ENTRY(drm_bacon_bo_gem
,
1087 bucket
->head
.next
, head
);
1088 if (time
- bo_gem
->free_time
<= 1)
1091 list_del(&bo_gem
->head
);
1093 drm_bacon_gem_bo_free(&bo_gem
->bo
);
1097 bufmgr
->time
= time
;
1100 static void drm_bacon_gem_bo_purge_vma_cache(drm_bacon_bufmgr
*bufmgr
)
1104 DBG("%s: cached=%d, open=%d, limit=%d\n", __FUNCTION__
,
1105 bufmgr
->vma_count
, bufmgr
->vma_open
, bufmgr
->vma_max
);
1107 if (bufmgr
->vma_max
< 0)
1110 /* We may need to evict a few entries in order to create new mmaps */
1111 limit
= bufmgr
->vma_max
- 2*bufmgr
->vma_open
;
1115 while (bufmgr
->vma_count
> limit
) {
1116 drm_bacon_bo_gem
*bo_gem
;
1118 bo_gem
= LIST_ENTRY(drm_bacon_bo_gem
,
1119 bufmgr
->vma_cache
.next
,
1121 assert(bo_gem
->map_count
== 0);
1122 list_delinit(&bo_gem
->vma_list
);
1124 if (bo_gem
->mem_virtual
) {
1125 drm_munmap(bo_gem
->mem_virtual
, bo_gem
->bo
.size
);
1126 bo_gem
->mem_virtual
= NULL
;
1127 bufmgr
->vma_count
--;
1129 if (bo_gem
->wc_virtual
) {
1130 drm_munmap(bo_gem
->wc_virtual
, bo_gem
->bo
.size
);
1131 bo_gem
->wc_virtual
= NULL
;
1132 bufmgr
->vma_count
--;
1134 if (bo_gem
->gtt_virtual
) {
1135 drm_munmap(bo_gem
->gtt_virtual
, bo_gem
->bo
.size
);
1136 bo_gem
->gtt_virtual
= NULL
;
1137 bufmgr
->vma_count
--;
1142 static void drm_bacon_gem_bo_close_vma(drm_bacon_bufmgr
*bufmgr
,
1143 drm_bacon_bo_gem
*bo_gem
)
1146 list_addtail(&bo_gem
->vma_list
, &bufmgr
->vma_cache
);
1147 if (bo_gem
->mem_virtual
)
1148 bufmgr
->vma_count
++;
1149 if (bo_gem
->wc_virtual
)
1150 bufmgr
->vma_count
++;
1151 if (bo_gem
->gtt_virtual
)
1152 bufmgr
->vma_count
++;
1153 drm_bacon_gem_bo_purge_vma_cache(bufmgr
);
1156 static void drm_bacon_gem_bo_open_vma(drm_bacon_bufmgr
*bufmgr
,
1157 drm_bacon_bo_gem
*bo_gem
)
1160 list_del(&bo_gem
->vma_list
);
1161 if (bo_gem
->mem_virtual
)
1162 bufmgr
->vma_count
--;
1163 if (bo_gem
->wc_virtual
)
1164 bufmgr
->vma_count
--;
1165 if (bo_gem
->gtt_virtual
)
1166 bufmgr
->vma_count
--;
1167 drm_bacon_gem_bo_purge_vma_cache(bufmgr
);
1171 drm_bacon_gem_bo_unreference_final(drm_bacon_bo
*bo
, time_t time
)
1173 drm_bacon_bufmgr
*bufmgr
= bo
->bufmgr
;
1174 drm_bacon_bo_gem
*bo_gem
= (drm_bacon_bo_gem
*) bo
;
1175 struct drm_bacon_gem_bo_bucket
*bucket
;
1178 /* Unreference all the target buffers */
1179 for (i
= 0; i
< bo_gem
->reloc_count
; i
++) {
1180 if (bo_gem
->reloc_target_info
[i
].bo
!= bo
) {
1181 drm_bacon_gem_bo_unreference_locked_timed(bo_gem
->
1182 reloc_target_info
[i
].bo
,
1186 for (i
= 0; i
< bo_gem
->softpin_target_count
; i
++)
1187 drm_bacon_gem_bo_unreference_locked_timed(bo_gem
->softpin_target
[i
],
1190 bo_gem
->reloc_count
= 0;
1191 bo_gem
->used_as_reloc_target
= false;
1192 bo_gem
->softpin_target_count
= 0;
1194 DBG("bo_unreference final: %d (%s)\n",
1195 bo_gem
->gem_handle
, bo_gem
->name
);
1197 /* release memory associated with this object */
1198 if (bo_gem
->reloc_target_info
) {
1199 free(bo_gem
->reloc_target_info
);
1200 bo_gem
->reloc_target_info
= NULL
;
1202 if (bo_gem
->relocs
) {
1203 free(bo_gem
->relocs
);
1204 bo_gem
->relocs
= NULL
;
1206 if (bo_gem
->softpin_target
) {
1207 free(bo_gem
->softpin_target
);
1208 bo_gem
->softpin_target
= NULL
;
1209 bo_gem
->softpin_target_size
= 0;
1212 /* Clear any left-over mappings */
1213 if (bo_gem
->map_count
) {
1214 DBG("bo freed with non-zero map-count %d\n", bo_gem
->map_count
);
1215 bo_gem
->map_count
= 0;
1216 drm_bacon_gem_bo_close_vma(bufmgr
, bo_gem
);
1217 drm_bacon_gem_bo_mark_mmaps_incoherent(bo
);
1220 bucket
= drm_bacon_gem_bo_bucket_for_size(bufmgr
, bo
->size
);
1221 /* Put the buffer into our internal cache for reuse if we can. */
1222 if (bufmgr
->bo_reuse
&& bo_gem
->reusable
&& bucket
!= NULL
&&
1223 drm_bacon_gem_bo_madvise_internal(bufmgr
, bo_gem
,
1224 I915_MADV_DONTNEED
)) {
1225 bo_gem
->free_time
= time
;
1227 bo_gem
->name
= NULL
;
1228 bo_gem
->validate_index
= -1;
1230 list_addtail(&bo_gem
->head
, &bucket
->head
);
1232 drm_bacon_gem_bo_free(bo
);
1236 static void drm_bacon_gem_bo_unreference_locked_timed(drm_bacon_bo
*bo
,
1239 drm_bacon_bo_gem
*bo_gem
= (drm_bacon_bo_gem
*) bo
;
1241 assert(p_atomic_read(&bo_gem
->refcount
) > 0);
1242 if (p_atomic_dec_zero(&bo_gem
->refcount
))
1243 drm_bacon_gem_bo_unreference_final(bo
, time
);
1247 drm_bacon_bo_unreference(drm_bacon_bo
*bo
)
1249 drm_bacon_bo_gem
*bo_gem
= (drm_bacon_bo_gem
*) bo
;
1254 assert(p_atomic_read(&bo_gem
->refcount
) > 0);
1256 if (atomic_add_unless(&bo_gem
->refcount
, -1, 1)) {
1257 drm_bacon_bufmgr
*bufmgr
= bo
->bufmgr
;
1258 struct timespec time
;
1260 clock_gettime(CLOCK_MONOTONIC
, &time
);
1262 pthread_mutex_lock(&bufmgr
->lock
);
1264 if (p_atomic_dec_zero(&bo_gem
->refcount
)) {
1265 drm_bacon_gem_bo_unreference_final(bo
, time
.tv_sec
);
1266 drm_bacon_gem_cleanup_bo_cache(bufmgr
, time
.tv_sec
);
1269 pthread_mutex_unlock(&bufmgr
->lock
);
1274 drm_bacon_bo_map(drm_bacon_bo
*bo
, int write_enable
)
1276 drm_bacon_bufmgr
*bufmgr
= bo
->bufmgr
;
1277 drm_bacon_bo_gem
*bo_gem
= (drm_bacon_bo_gem
*) bo
;
1278 struct drm_i915_gem_set_domain set_domain
;
1281 if (bo_gem
->is_userptr
) {
1282 /* Return the same user ptr */
1283 bo
->virtual = bo_gem
->user_virtual
;
1287 pthread_mutex_lock(&bufmgr
->lock
);
1289 if (bo_gem
->map_count
++ == 0)
1290 drm_bacon_gem_bo_open_vma(bufmgr
, bo_gem
);
1292 if (!bo_gem
->mem_virtual
) {
1293 struct drm_i915_gem_mmap mmap_arg
;
1295 DBG("bo_map: %d (%s), map_count=%d\n",
1296 bo_gem
->gem_handle
, bo_gem
->name
, bo_gem
->map_count
);
1299 mmap_arg
.handle
= bo_gem
->gem_handle
;
1300 mmap_arg
.size
= bo
->size
;
1301 ret
= drmIoctl(bufmgr
->fd
,
1302 DRM_IOCTL_I915_GEM_MMAP
,
1306 DBG("%s:%d: Error mapping buffer %d (%s): %s .\n",
1307 __FILE__
, __LINE__
, bo_gem
->gem_handle
,
1308 bo_gem
->name
, strerror(errno
));
1309 if (--bo_gem
->map_count
== 0)
1310 drm_bacon_gem_bo_close_vma(bufmgr
, bo_gem
);
1311 pthread_mutex_unlock(&bufmgr
->lock
);
1314 VG(VALGRIND_MALLOCLIKE_BLOCK(mmap_arg
.addr_ptr
, mmap_arg
.size
, 0, 1));
1315 bo_gem
->mem_virtual
= (void *)(uintptr_t) mmap_arg
.addr_ptr
;
1317 DBG("bo_map: %d (%s) -> %p\n", bo_gem
->gem_handle
, bo_gem
->name
,
1318 bo_gem
->mem_virtual
);
1319 bo
->virtual = bo_gem
->mem_virtual
;
1321 memclear(set_domain
);
1322 set_domain
.handle
= bo_gem
->gem_handle
;
1323 set_domain
.read_domains
= I915_GEM_DOMAIN_CPU
;
1325 set_domain
.write_domain
= I915_GEM_DOMAIN_CPU
;
1327 set_domain
.write_domain
= 0;
1328 ret
= drmIoctl(bufmgr
->fd
,
1329 DRM_IOCTL_I915_GEM_SET_DOMAIN
,
1332 DBG("%s:%d: Error setting to CPU domain %d: %s\n",
1333 __FILE__
, __LINE__
, bo_gem
->gem_handle
,
1338 bo_gem
->mapped_cpu_write
= true;
1340 drm_bacon_gem_bo_mark_mmaps_incoherent(bo
);
1341 VG(VALGRIND_MAKE_MEM_DEFINED(bo_gem
->mem_virtual
, bo
->size
));
1342 pthread_mutex_unlock(&bufmgr
->lock
);
1348 map_gtt(drm_bacon_bo
*bo
)
1350 drm_bacon_bufmgr
*bufmgr
= bo
->bufmgr
;
1351 drm_bacon_bo_gem
*bo_gem
= (drm_bacon_bo_gem
*) bo
;
1354 if (bo_gem
->is_userptr
)
1357 if (bo_gem
->map_count
++ == 0)
1358 drm_bacon_gem_bo_open_vma(bufmgr
, bo_gem
);
1360 /* Get a mapping of the buffer if we haven't before. */
1361 if (bo_gem
->gtt_virtual
== NULL
) {
1362 struct drm_i915_gem_mmap_gtt mmap_arg
;
1364 DBG("bo_map_gtt: mmap %d (%s), map_count=%d\n",
1365 bo_gem
->gem_handle
, bo_gem
->name
, bo_gem
->map_count
);
1368 mmap_arg
.handle
= bo_gem
->gem_handle
;
1370 /* Get the fake offset back... */
1371 ret
= drmIoctl(bufmgr
->fd
,
1372 DRM_IOCTL_I915_GEM_MMAP_GTT
,
1376 DBG("%s:%d: Error preparing buffer map %d (%s): %s .\n",
1378 bo_gem
->gem_handle
, bo_gem
->name
,
1380 if (--bo_gem
->map_count
== 0)
1381 drm_bacon_gem_bo_close_vma(bufmgr
, bo_gem
);
1386 bo_gem
->gtt_virtual
= drm_mmap(0, bo
->size
, PROT_READ
| PROT_WRITE
,
1387 MAP_SHARED
, bufmgr
->fd
,
1389 if (bo_gem
->gtt_virtual
== MAP_FAILED
) {
1390 bo_gem
->gtt_virtual
= NULL
;
1392 DBG("%s:%d: Error mapping buffer %d (%s): %s .\n",
1394 bo_gem
->gem_handle
, bo_gem
->name
,
1396 if (--bo_gem
->map_count
== 0)
1397 drm_bacon_gem_bo_close_vma(bufmgr
, bo_gem
);
1402 bo
->virtual = bo_gem
->gtt_virtual
;
1404 DBG("bo_map_gtt: %d (%s) -> %p\n", bo_gem
->gem_handle
, bo_gem
->name
,
1405 bo_gem
->gtt_virtual
);
1411 drm_bacon_gem_bo_map_gtt(drm_bacon_bo
*bo
)
1413 drm_bacon_bufmgr
*bufmgr
= bo
->bufmgr
;
1414 drm_bacon_bo_gem
*bo_gem
= (drm_bacon_bo_gem
*) bo
;
1415 struct drm_i915_gem_set_domain set_domain
;
1418 pthread_mutex_lock(&bufmgr
->lock
);
1422 pthread_mutex_unlock(&bufmgr
->lock
);
1426 /* Now move it to the GTT domain so that the GPU and CPU
1427 * caches are flushed and the GPU isn't actively using the
1430 * The pagefault handler does this domain change for us when
1431 * it has unbound the BO from the GTT, but it's up to us to
1432 * tell it when we're about to use things if we had done
1433 * rendering and it still happens to be bound to the GTT.
1435 memclear(set_domain
);
1436 set_domain
.handle
= bo_gem
->gem_handle
;
1437 set_domain
.read_domains
= I915_GEM_DOMAIN_GTT
;
1438 set_domain
.write_domain
= I915_GEM_DOMAIN_GTT
;
1439 ret
= drmIoctl(bufmgr
->fd
,
1440 DRM_IOCTL_I915_GEM_SET_DOMAIN
,
1443 DBG("%s:%d: Error setting domain %d: %s\n",
1444 __FILE__
, __LINE__
, bo_gem
->gem_handle
,
1448 drm_bacon_gem_bo_mark_mmaps_incoherent(bo
);
1449 VG(VALGRIND_MAKE_MEM_DEFINED(bo_gem
->gtt_virtual
, bo
->size
));
1450 pthread_mutex_unlock(&bufmgr
->lock
);
1456 * Performs a mapping of the buffer object like the normal GTT
1457 * mapping, but avoids waiting for the GPU to be done reading from or
1458 * rendering to the buffer.
1460 * This is used in the implementation of GL_ARB_map_buffer_range: The
1461 * user asks to create a buffer, then does a mapping, fills some
1462 * space, runs a drawing command, then asks to map it again without
1463 * synchronizing because it guarantees that it won't write over the
1464 * data that the GPU is busy using (or, more specifically, that if it
1465 * does write over the data, it acknowledges that rendering is
1470 drm_bacon_gem_bo_map_unsynchronized(drm_bacon_bo
*bo
)
1472 drm_bacon_bufmgr
*bufmgr
= bo
->bufmgr
;
1473 #ifdef HAVE_VALGRIND
1474 drm_bacon_bo_gem
*bo_gem
= (drm_bacon_bo_gem
*) bo
;
1478 /* If the CPU cache isn't coherent with the GTT, then use a
1479 * regular synchronized mapping. The problem is that we don't
1480 * track where the buffer was last used on the CPU side in
1481 * terms of drm_bacon_bo_map vs drm_bacon_gem_bo_map_gtt, so
1482 * we would potentially corrupt the buffer even when the user
1483 * does reasonable things.
1485 if (!bufmgr
->has_llc
)
1486 return drm_bacon_gem_bo_map_gtt(bo
);
1488 pthread_mutex_lock(&bufmgr
->lock
);
1492 drm_bacon_gem_bo_mark_mmaps_incoherent(bo
);
1493 VG(VALGRIND_MAKE_MEM_DEFINED(bo_gem
->gtt_virtual
, bo
->size
));
1496 pthread_mutex_unlock(&bufmgr
->lock
);
1502 drm_bacon_bo_unmap(drm_bacon_bo
*bo
)
1504 drm_bacon_bufmgr
*bufmgr
= bo
->bufmgr
;
1505 drm_bacon_bo_gem
*bo_gem
= (drm_bacon_bo_gem
*) bo
;
1511 if (bo_gem
->is_userptr
)
1514 pthread_mutex_lock(&bufmgr
->lock
);
1516 if (bo_gem
->map_count
<= 0) {
1517 DBG("attempted to unmap an unmapped bo\n");
1518 pthread_mutex_unlock(&bufmgr
->lock
);
1519 /* Preserve the old behaviour of just treating this as a
1520 * no-op rather than reporting the error.
1525 if (bo_gem
->mapped_cpu_write
) {
1526 struct drm_i915_gem_sw_finish sw_finish
;
1528 /* Cause a flush to happen if the buffer's pinned for
1529 * scanout, so the results show up in a timely manner.
1530 * Unlike GTT set domains, this only does work if the
1531 * buffer should be scanout-related.
1533 memclear(sw_finish
);
1534 sw_finish
.handle
= bo_gem
->gem_handle
;
1535 ret
= drmIoctl(bufmgr
->fd
,
1536 DRM_IOCTL_I915_GEM_SW_FINISH
,
1538 ret
= ret
== -1 ? -errno
: 0;
1540 bo_gem
->mapped_cpu_write
= false;
1543 /* We need to unmap after every innovation as we cannot track
1544 * an open vma for every bo as that will exhaust the system
1545 * limits and cause later failures.
1547 if (--bo_gem
->map_count
== 0) {
1548 drm_bacon_gem_bo_close_vma(bufmgr
, bo_gem
);
1549 drm_bacon_gem_bo_mark_mmaps_incoherent(bo
);
1552 pthread_mutex_unlock(&bufmgr
->lock
);
1558 drm_bacon_bo_subdata(drm_bacon_bo
*bo
, unsigned long offset
,
1559 unsigned long size
, const void *data
)
1561 drm_bacon_bufmgr
*bufmgr
= bo
->bufmgr
;
1562 drm_bacon_bo_gem
*bo_gem
= (drm_bacon_bo_gem
*) bo
;
1563 struct drm_i915_gem_pwrite pwrite
;
1566 if (bo_gem
->is_userptr
)
1570 pwrite
.handle
= bo_gem
->gem_handle
;
1571 pwrite
.offset
= offset
;
1573 pwrite
.data_ptr
= (uint64_t) (uintptr_t) data
;
1574 ret
= drmIoctl(bufmgr
->fd
,
1575 DRM_IOCTL_I915_GEM_PWRITE
,
1579 DBG("%s:%d: Error writing data to buffer %d: (%d %d) %s .\n",
1580 __FILE__
, __LINE__
, bo_gem
->gem_handle
, (int)offset
,
1581 (int)size
, strerror(errno
));
1588 drm_bacon_bo_get_subdata(drm_bacon_bo
*bo
, unsigned long offset
,
1589 unsigned long size
, void *data
)
1591 drm_bacon_bufmgr
*bufmgr
= bo
->bufmgr
;
1592 drm_bacon_bo_gem
*bo_gem
= (drm_bacon_bo_gem
*) bo
;
1593 struct drm_i915_gem_pread pread
;
1596 if (bo_gem
->is_userptr
)
1600 pread
.handle
= bo_gem
->gem_handle
;
1601 pread
.offset
= offset
;
1603 pread
.data_ptr
= (uint64_t) (uintptr_t) data
;
1604 ret
= drmIoctl(bufmgr
->fd
,
1605 DRM_IOCTL_I915_GEM_PREAD
,
1609 DBG("%s:%d: Error reading data from buffer %d: (%d %d) %s .\n",
1610 __FILE__
, __LINE__
, bo_gem
->gem_handle
, (int)offset
,
1611 (int)size
, strerror(errno
));
1617 /** Waits for all GPU rendering with the object to have completed. */
1619 drm_bacon_bo_wait_rendering(drm_bacon_bo
*bo
)
1621 drm_bacon_gem_bo_start_gtt_access(bo
, 1);
1625 * Waits on a BO for the given amount of time.
1627 * @bo: buffer object to wait for
1628 * @timeout_ns: amount of time to wait in nanoseconds.
1629 * If value is less than 0, an infinite wait will occur.
1631 * Returns 0 if the wait was successful ie. the last batch referencing the
1632 * object has completed within the allotted time. Otherwise some negative return
1633 * value describes the error. Of particular interest is -ETIME when the wait has
1634 * failed to yield the desired result.
1636 * Similar to drm_bacon_gem_bo_wait_rendering except a timeout parameter allows
1637 * the operation to give up after a certain amount of time. Another subtle
1638 * difference is the internal locking semantics are different (this variant does
1639 * not hold the lock for the duration of the wait). This makes the wait subject
1640 * to a larger userspace race window.
1642 * The implementation shall wait until the object is no longer actively
1643 * referenced within a batch buffer at the time of the call. The wait will
1644 * not guarantee that the buffer is re-issued via another thread, or an flinked
1645 * handle. Userspace must make sure this race does not occur if such precision
1648 * Note that some kernels have broken the inifite wait for negative values
1649 * promise, upgrade to latest stable kernels if this is the case.
1652 drm_bacon_gem_bo_wait(drm_bacon_bo
*bo
, int64_t timeout_ns
)
1654 drm_bacon_bufmgr
*bufmgr
= bo
->bufmgr
;
1655 drm_bacon_bo_gem
*bo_gem
= (drm_bacon_bo_gem
*) bo
;
1656 struct drm_i915_gem_wait wait
;
1660 wait
.bo_handle
= bo_gem
->gem_handle
;
1661 wait
.timeout_ns
= timeout_ns
;
1662 ret
= drmIoctl(bufmgr
->fd
, DRM_IOCTL_I915_GEM_WAIT
, &wait
);
1670 * Sets the object to the GTT read and possibly write domain, used by the X
1671 * 2D driver in the absence of kernel support to do drm_bacon_gem_bo_map_gtt().
1673 * In combination with drm_bacon_gem_bo_pin() and manual fence management, we
1674 * can do tiled pixmaps this way.
1677 drm_bacon_gem_bo_start_gtt_access(drm_bacon_bo
*bo
, int write_enable
)
1679 drm_bacon_bufmgr
*bufmgr
= bo
->bufmgr
;
1680 drm_bacon_bo_gem
*bo_gem
= (drm_bacon_bo_gem
*) bo
;
1681 struct drm_i915_gem_set_domain set_domain
;
1684 memclear(set_domain
);
1685 set_domain
.handle
= bo_gem
->gem_handle
;
1686 set_domain
.read_domains
= I915_GEM_DOMAIN_GTT
;
1687 set_domain
.write_domain
= write_enable
? I915_GEM_DOMAIN_GTT
: 0;
1688 ret
= drmIoctl(bufmgr
->fd
,
1689 DRM_IOCTL_I915_GEM_SET_DOMAIN
,
1692 DBG("%s:%d: Error setting memory domains %d (%08x %08x): %s .\n",
1693 __FILE__
, __LINE__
, bo_gem
->gem_handle
,
1694 set_domain
.read_domains
, set_domain
.write_domain
,
1700 drm_bacon_bufmgr_gem_destroy(drm_bacon_bufmgr
*bufmgr
)
1702 struct drm_gem_close close_bo
;
1705 free(bufmgr
->exec2_objects
);
1706 free(bufmgr
->exec_bos
);
1708 pthread_mutex_destroy(&bufmgr
->lock
);
1710 /* Free any cached buffer objects we were going to reuse */
1711 for (i
= 0; i
< bufmgr
->num_buckets
; i
++) {
1712 struct drm_bacon_gem_bo_bucket
*bucket
=
1713 &bufmgr
->cache_bucket
[i
];
1714 drm_bacon_bo_gem
*bo_gem
;
1716 while (!list_empty(&bucket
->head
)) {
1717 bo_gem
= LIST_ENTRY(drm_bacon_bo_gem
,
1718 bucket
->head
.next
, head
);
1719 list_del(&bo_gem
->head
);
1721 drm_bacon_gem_bo_free(&bo_gem
->bo
);
1725 /* Release userptr bo kept hanging around for optimisation. */
1726 if (bufmgr
->userptr_active
.ptr
) {
1728 close_bo
.handle
= bufmgr
->userptr_active
.handle
;
1729 ret
= drmIoctl(bufmgr
->fd
, DRM_IOCTL_GEM_CLOSE
, &close_bo
);
1730 free(bufmgr
->userptr_active
.ptr
);
1733 "Failed to release test userptr object! (%d) "
1734 "i915 kernel driver may not be sane!\n", errno
);
1741 * Adds the target buffer to the validation list and adds the relocation
1742 * to the reloc_buffer's relocation list.
1744 * The relocation entry at the given offset must already contain the
1745 * precomputed relocation value, because the kernel will optimize out
1746 * the relocation entry write when the buffer hasn't moved from the
1747 * last known offset in target_bo.
1750 do_bo_emit_reloc(drm_bacon_bo
*bo
, uint32_t offset
,
1751 drm_bacon_bo
*target_bo
, uint32_t target_offset
,
1752 uint32_t read_domains
, uint32_t write_domain
)
1754 drm_bacon_bufmgr
*bufmgr
= bo
->bufmgr
;
1755 drm_bacon_bo_gem
*bo_gem
= (drm_bacon_bo_gem
*) bo
;
1756 drm_bacon_bo_gem
*target_bo_gem
= (drm_bacon_bo_gem
*) target_bo
;
1758 if (bo_gem
->has_error
)
1761 if (target_bo_gem
->has_error
) {
1762 bo_gem
->has_error
= true;
1766 /* Create a new relocation list if needed */
1767 if (bo_gem
->relocs
== NULL
&& drm_bacon_setup_reloc_list(bo
))
1770 /* Check overflow */
1771 assert(bo_gem
->reloc_count
< bufmgr
->max_relocs
);
1774 assert(offset
<= bo
->size
- 4);
1775 assert((write_domain
& (write_domain
- 1)) == 0);
1777 /* Make sure that we're not adding a reloc to something whose size has
1778 * already been accounted for.
1780 assert(!bo_gem
->used_as_reloc_target
);
1781 if (target_bo_gem
!= bo_gem
) {
1782 target_bo_gem
->used_as_reloc_target
= true;
1783 bo_gem
->reloc_tree_size
+= target_bo_gem
->reloc_tree_size
;
1786 bo_gem
->reloc_target_info
[bo_gem
->reloc_count
].bo
= target_bo
;
1787 if (target_bo
!= bo
)
1788 drm_bacon_bo_reference(target_bo
);
1790 bo_gem
->relocs
[bo_gem
->reloc_count
].offset
= offset
;
1791 bo_gem
->relocs
[bo_gem
->reloc_count
].delta
= target_offset
;
1792 bo_gem
->relocs
[bo_gem
->reloc_count
].target_handle
=
1793 target_bo_gem
->gem_handle
;
1794 bo_gem
->relocs
[bo_gem
->reloc_count
].read_domains
= read_domains
;
1795 bo_gem
->relocs
[bo_gem
->reloc_count
].write_domain
= write_domain
;
1796 bo_gem
->relocs
[bo_gem
->reloc_count
].presumed_offset
= target_bo
->offset64
;
1797 bo_gem
->reloc_count
++;
1803 drm_bacon_gem_bo_add_softpin_target(drm_bacon_bo
*bo
, drm_bacon_bo
*target_bo
)
1805 drm_bacon_bufmgr
*bufmgr
= bo
->bufmgr
;
1806 drm_bacon_bo_gem
*bo_gem
= (drm_bacon_bo_gem
*) bo
;
1807 drm_bacon_bo_gem
*target_bo_gem
= (drm_bacon_bo_gem
*) target_bo
;
1809 if (bo_gem
->has_error
)
1812 if (target_bo_gem
->has_error
) {
1813 bo_gem
->has_error
= true;
1817 if (!(target_bo_gem
->kflags
& EXEC_OBJECT_PINNED
))
1819 if (target_bo_gem
== bo_gem
)
1822 if (bo_gem
->softpin_target_count
== bo_gem
->softpin_target_size
) {
1823 int new_size
= bo_gem
->softpin_target_size
* 2;
1825 new_size
= bufmgr
->max_relocs
;
1827 bo_gem
->softpin_target
= realloc(bo_gem
->softpin_target
, new_size
*
1828 sizeof(drm_bacon_bo
*));
1829 if (!bo_gem
->softpin_target
)
1832 bo_gem
->softpin_target_size
= new_size
;
1834 bo_gem
->softpin_target
[bo_gem
->softpin_target_count
] = target_bo
;
1835 drm_bacon_bo_reference(target_bo
);
1836 bo_gem
->softpin_target_count
++;
1842 drm_bacon_bo_emit_reloc(drm_bacon_bo
*bo
, uint32_t offset
,
1843 drm_bacon_bo
*target_bo
, uint32_t target_offset
,
1844 uint32_t read_domains
, uint32_t write_domain
)
1846 drm_bacon_bo_gem
*target_bo_gem
= (drm_bacon_bo_gem
*)target_bo
;
1848 if (target_bo_gem
->kflags
& EXEC_OBJECT_PINNED
)
1849 return drm_bacon_gem_bo_add_softpin_target(bo
, target_bo
);
1851 return do_bo_emit_reloc(bo
, offset
, target_bo
, target_offset
,
1852 read_domains
, write_domain
);
1856 drm_bacon_gem_bo_get_reloc_count(drm_bacon_bo
*bo
)
1858 drm_bacon_bo_gem
*bo_gem
= (drm_bacon_bo_gem
*) bo
;
1860 return bo_gem
->reloc_count
;
1864 * Removes existing relocation entries in the BO after "start".
1866 * This allows a user to avoid a two-step process for state setup with
1867 * counting up all the buffer objects and doing a
1868 * drm_bacon_bufmgr_check_aperture_space() before emitting any of the
1869 * relocations for the state setup. Instead, save the state of the
1870 * batchbuffer including drm_bacon_gem_get_reloc_count(), emit all the
1871 * state, and then check if it still fits in the aperture.
1873 * Any further drm_bacon_bufmgr_check_aperture_space() queries
1874 * involving this buffer in the tree are undefined after this call.
1876 * This also removes all softpinned targets being referenced by the BO.
1879 drm_bacon_gem_bo_clear_relocs(drm_bacon_bo
*bo
, int start
)
1881 drm_bacon_bufmgr
*bufmgr
= bo
->bufmgr
;
1882 drm_bacon_bo_gem
*bo_gem
= (drm_bacon_bo_gem
*) bo
;
1884 struct timespec time
;
1886 clock_gettime(CLOCK_MONOTONIC
, &time
);
1888 assert(bo_gem
->reloc_count
>= start
);
1890 /* Unreference the cleared target buffers */
1891 pthread_mutex_lock(&bufmgr
->lock
);
1893 for (i
= start
; i
< bo_gem
->reloc_count
; i
++) {
1894 drm_bacon_bo_gem
*target_bo_gem
= (drm_bacon_bo_gem
*) bo_gem
->reloc_target_info
[i
].bo
;
1895 if (&target_bo_gem
->bo
!= bo
) {
1896 drm_bacon_gem_bo_unreference_locked_timed(&target_bo_gem
->bo
,
1900 bo_gem
->reloc_count
= start
;
1902 for (i
= 0; i
< bo_gem
->softpin_target_count
; i
++) {
1903 drm_bacon_bo_gem
*target_bo_gem
= (drm_bacon_bo_gem
*) bo_gem
->softpin_target
[i
];
1904 drm_bacon_gem_bo_unreference_locked_timed(&target_bo_gem
->bo
, time
.tv_sec
);
1906 bo_gem
->softpin_target_count
= 0;
1908 pthread_mutex_unlock(&bufmgr
->lock
);
1913 drm_bacon_gem_bo_process_reloc2(drm_bacon_bo
*bo
)
1915 drm_bacon_bo_gem
*bo_gem
= (drm_bacon_bo_gem
*)bo
;
1918 if (bo_gem
->relocs
== NULL
&& bo_gem
->softpin_target
== NULL
)
1921 for (i
= 0; i
< bo_gem
->reloc_count
; i
++) {
1922 drm_bacon_bo
*target_bo
= bo_gem
->reloc_target_info
[i
].bo
;
1924 if (target_bo
== bo
)
1927 drm_bacon_gem_bo_mark_mmaps_incoherent(bo
);
1929 /* Continue walking the tree depth-first. */
1930 drm_bacon_gem_bo_process_reloc2(target_bo
);
1932 /* Add the target to the validate list */
1933 drm_bacon_add_validate_buffer2(target_bo
);
1936 for (i
= 0; i
< bo_gem
->softpin_target_count
; i
++) {
1937 drm_bacon_bo
*target_bo
= bo_gem
->softpin_target
[i
];
1939 if (target_bo
== bo
)
1942 drm_bacon_gem_bo_mark_mmaps_incoherent(bo
);
1943 drm_bacon_gem_bo_process_reloc2(target_bo
);
1944 drm_bacon_add_validate_buffer2(target_bo
);
1949 drm_bacon_update_buffer_offsets2 (drm_bacon_bufmgr
*bufmgr
)
1953 for (i
= 0; i
< bufmgr
->exec_count
; i
++) {
1954 drm_bacon_bo
*bo
= bufmgr
->exec_bos
[i
];
1955 drm_bacon_bo_gem
*bo_gem
= (drm_bacon_bo_gem
*)bo
;
1957 /* Update the buffer offset */
1958 if (bufmgr
->exec2_objects
[i
].offset
!= bo
->offset64
) {
1959 /* If we're seeing softpinned object here it means that the kernel
1960 * has relocated our object... Indicating a programming error
1962 assert(!(bo_gem
->kflags
& EXEC_OBJECT_PINNED
));
1963 DBG("BO %d (%s) migrated: 0x%08x %08x -> 0x%08x %08x\n",
1964 bo_gem
->gem_handle
, bo_gem
->name
,
1965 upper_32_bits(bo
->offset64
),
1966 lower_32_bits(bo
->offset64
),
1967 upper_32_bits(bufmgr
->exec2_objects
[i
].offset
),
1968 lower_32_bits(bufmgr
->exec2_objects
[i
].offset
));
1969 bo
->offset64
= bufmgr
->exec2_objects
[i
].offset
;
1975 do_exec2(drm_bacon_bo
*bo
, int used
, drm_bacon_context
*ctx
,
1976 int in_fence
, int *out_fence
,
1979 drm_bacon_bufmgr
*bufmgr
= bo
->bufmgr
;
1980 struct drm_i915_gem_execbuffer2 execbuf
;
1984 if (to_bo_gem(bo
)->has_error
)
1987 pthread_mutex_lock(&bufmgr
->lock
);
1988 /* Update indices and set up the validate list. */
1989 drm_bacon_gem_bo_process_reloc2(bo
);
1991 /* Add the batch buffer to the validation list. There are no relocations
1994 drm_bacon_add_validate_buffer2(bo
);
1997 execbuf
.buffers_ptr
= (uintptr_t)bufmgr
->exec2_objects
;
1998 execbuf
.buffer_count
= bufmgr
->exec_count
;
1999 execbuf
.batch_start_offset
= 0;
2000 execbuf
.batch_len
= used
;
2001 execbuf
.cliprects_ptr
= 0;
2002 execbuf
.num_cliprects
= 0;
2005 execbuf
.flags
= flags
;
2007 i915_execbuffer2_set_context_id(execbuf
, 0);
2009 i915_execbuffer2_set_context_id(execbuf
, ctx
->ctx_id
);
2011 if (in_fence
!= -1) {
2012 execbuf
.rsvd2
= in_fence
;
2013 execbuf
.flags
|= I915_EXEC_FENCE_IN
;
2015 if (out_fence
!= NULL
) {
2017 execbuf
.flags
|= I915_EXEC_FENCE_OUT
;
2020 if (bufmgr
->no_exec
)
2021 goto skip_execution
;
2023 ret
= drmIoctl(bufmgr
->fd
,
2024 DRM_IOCTL_I915_GEM_EXECBUFFER2_WR
,
2028 if (ret
== -ENOSPC
) {
2029 DBG("Execbuffer fails to pin. "
2030 "Estimate: %u. Actual: %u. Available: %u\n",
2031 drm_bacon_gem_estimate_batch_space(bufmgr
->exec_bos
,
2032 bufmgr
->exec_count
),
2033 drm_bacon_gem_compute_batch_space(bufmgr
->exec_bos
,
2034 bufmgr
->exec_count
),
2035 (unsigned int) bufmgr
->gtt_size
);
2038 drm_bacon_update_buffer_offsets2(bufmgr
);
2040 if (ret
== 0 && out_fence
!= NULL
)
2041 *out_fence
= execbuf
.rsvd2
>> 32;
2044 if (INTEL_DEBUG
& DEBUG_BUFMGR
)
2045 drm_bacon_gem_dump_validation_list(bufmgr
);
2047 for (i
= 0; i
< bufmgr
->exec_count
; i
++) {
2048 drm_bacon_bo_gem
*bo_gem
= to_bo_gem(bufmgr
->exec_bos
[i
]);
2050 bo_gem
->idle
= false;
2052 /* Disconnect the buffer from the validate list */
2053 bo_gem
->validate_index
= -1;
2054 bufmgr
->exec_bos
[i
] = NULL
;
2056 bufmgr
->exec_count
= 0;
2057 pthread_mutex_unlock(&bufmgr
->lock
);
2063 drm_bacon_bo_exec(drm_bacon_bo
*bo
, int used
)
2065 return do_exec2(bo
, used
, NULL
, -1, NULL
, I915_EXEC_RENDER
);
2069 drm_bacon_bo_mrb_exec(drm_bacon_bo
*bo
, int used
, unsigned int flags
)
2071 return do_exec2(bo
, used
, NULL
, -1, NULL
, flags
);
2075 drm_bacon_gem_bo_context_exec(drm_bacon_bo
*bo
, drm_bacon_context
*ctx
,
2076 int used
, unsigned int flags
)
2078 return do_exec2(bo
, used
, ctx
, -1, NULL
, flags
);
2082 drm_bacon_gem_bo_fence_exec(drm_bacon_bo
*bo
,
2083 drm_bacon_context
*ctx
,
2089 return do_exec2(bo
, used
, ctx
, in_fence
, out_fence
, flags
);
2093 drm_bacon_gem_bo_set_tiling_internal(drm_bacon_bo
*bo
,
2094 uint32_t tiling_mode
,
2097 drm_bacon_bufmgr
*bufmgr
= bo
->bufmgr
;
2098 drm_bacon_bo_gem
*bo_gem
= (drm_bacon_bo_gem
*) bo
;
2099 struct drm_i915_gem_set_tiling set_tiling
;
2102 if (bo_gem
->global_name
== 0 &&
2103 tiling_mode
== bo_gem
->tiling_mode
&&
2104 stride
== bo_gem
->stride
)
2107 memset(&set_tiling
, 0, sizeof(set_tiling
));
2109 /* set_tiling is slightly broken and overwrites the
2110 * input on the error path, so we have to open code
2113 set_tiling
.handle
= bo_gem
->gem_handle
;
2114 set_tiling
.tiling_mode
= tiling_mode
;
2115 set_tiling
.stride
= stride
;
2117 ret
= ioctl(bufmgr
->fd
,
2118 DRM_IOCTL_I915_GEM_SET_TILING
,
2120 } while (ret
== -1 && (errno
== EINTR
|| errno
== EAGAIN
));
2124 bo_gem
->tiling_mode
= set_tiling
.tiling_mode
;
2125 bo_gem
->swizzle_mode
= set_tiling
.swizzle_mode
;
2126 bo_gem
->stride
= set_tiling
.stride
;
2131 drm_bacon_bo_set_tiling(drm_bacon_bo
*bo
, uint32_t * tiling_mode
,
2134 drm_bacon_bufmgr
*bufmgr
= bo
->bufmgr
;
2135 drm_bacon_bo_gem
*bo_gem
= (drm_bacon_bo_gem
*) bo
;
2138 /* Tiling with userptr surfaces is not supported
2139 * on all hardware so refuse it for time being.
2141 if (bo_gem
->is_userptr
)
2144 /* Linear buffers have no stride. By ensuring that we only ever use
2145 * stride 0 with linear buffers, we simplify our code.
2147 if (*tiling_mode
== I915_TILING_NONE
)
2150 ret
= drm_bacon_gem_bo_set_tiling_internal(bo
, *tiling_mode
, stride
);
2152 drm_bacon_bo_gem_set_in_aperture_size(bufmgr
, bo_gem
, 0);
2154 *tiling_mode
= bo_gem
->tiling_mode
;
2159 drm_bacon_bo_get_tiling(drm_bacon_bo
*bo
, uint32_t * tiling_mode
,
2160 uint32_t *swizzle_mode
)
2162 drm_bacon_bo_gem
*bo_gem
= (drm_bacon_bo_gem
*) bo
;
2164 *tiling_mode
= bo_gem
->tiling_mode
;
2165 *swizzle_mode
= bo_gem
->swizzle_mode
;
2170 drm_bacon_bo_set_softpin_offset(drm_bacon_bo
*bo
, uint64_t offset
)
2172 drm_bacon_bo_gem
*bo_gem
= (drm_bacon_bo_gem
*) bo
;
2174 bo
->offset64
= offset
;
2175 bo_gem
->kflags
|= EXEC_OBJECT_PINNED
;
2181 drm_bacon_bo_gem_create_from_prime(drm_bacon_bufmgr
*bufmgr
, int prime_fd
, int size
)
2185 drm_bacon_bo_gem
*bo_gem
;
2186 struct drm_i915_gem_get_tiling get_tiling
;
2188 pthread_mutex_lock(&bufmgr
->lock
);
2189 ret
= drmPrimeFDToHandle(bufmgr
->fd
, prime_fd
, &handle
);
2191 DBG("create_from_prime: failed to obtain handle from fd: %s\n", strerror(errno
));
2192 pthread_mutex_unlock(&bufmgr
->lock
);
2197 * See if the kernel has already returned this buffer to us. Just as
2198 * for named buffers, we must not create two bo's pointing at the same
2201 HASH_FIND(handle_hh
, bufmgr
->handle_table
,
2202 &handle
, sizeof(handle
), bo_gem
);
2204 drm_bacon_bo_reference(&bo_gem
->bo
);
2208 bo_gem
= calloc(1, sizeof(*bo_gem
));
2212 p_atomic_set(&bo_gem
->refcount
, 1);
2213 list_inithead(&bo_gem
->vma_list
);
2215 /* Determine size of bo. The fd-to-handle ioctl really should
2216 * return the size, but it doesn't. If we have kernel 3.12 or
2217 * later, we can lseek on the prime fd to get the size. Older
2218 * kernels will just fail, in which case we fall back to the
2219 * provided (estimated or guess size). */
2220 ret
= lseek(prime_fd
, 0, SEEK_END
);
2222 bo_gem
->bo
.size
= ret
;
2224 bo_gem
->bo
.size
= size
;
2226 bo_gem
->bo
.handle
= handle
;
2227 bo_gem
->bo
.bufmgr
= bufmgr
;
2229 bo_gem
->gem_handle
= handle
;
2230 HASH_ADD(handle_hh
, bufmgr
->handle_table
,
2231 gem_handle
, sizeof(bo_gem
->gem_handle
), bo_gem
);
2233 bo_gem
->name
= "prime";
2234 bo_gem
->validate_index
= -1;
2235 bo_gem
->used_as_reloc_target
= false;
2236 bo_gem
->has_error
= false;
2237 bo_gem
->reusable
= false;
2239 memclear(get_tiling
);
2240 get_tiling
.handle
= bo_gem
->gem_handle
;
2241 if (drmIoctl(bufmgr
->fd
,
2242 DRM_IOCTL_I915_GEM_GET_TILING
,
2246 bo_gem
->tiling_mode
= get_tiling
.tiling_mode
;
2247 bo_gem
->swizzle_mode
= get_tiling
.swizzle_mode
;
2248 /* XXX stride is unknown */
2249 drm_bacon_bo_gem_set_in_aperture_size(bufmgr
, bo_gem
, 0);
2252 pthread_mutex_unlock(&bufmgr
->lock
);
2256 drm_bacon_gem_bo_free(&bo_gem
->bo
);
2257 pthread_mutex_unlock(&bufmgr
->lock
);
2262 drm_bacon_bo_gem_export_to_prime(drm_bacon_bo
*bo
, int *prime_fd
)
2264 drm_bacon_bufmgr
*bufmgr
= bo
->bufmgr
;
2265 drm_bacon_bo_gem
*bo_gem
= (drm_bacon_bo_gem
*) bo
;
2267 if (drmPrimeHandleToFD(bufmgr
->fd
, bo_gem
->gem_handle
,
2268 DRM_CLOEXEC
, prime_fd
) != 0)
2271 bo_gem
->reusable
= false;
2277 drm_bacon_bo_flink(drm_bacon_bo
*bo
, uint32_t *name
)
2279 drm_bacon_bufmgr
*bufmgr
= bo
->bufmgr
;
2280 drm_bacon_bo_gem
*bo_gem
= (drm_bacon_bo_gem
*) bo
;
2282 if (!bo_gem
->global_name
) {
2283 struct drm_gem_flink flink
;
2286 flink
.handle
= bo_gem
->gem_handle
;
2287 if (drmIoctl(bufmgr
->fd
, DRM_IOCTL_GEM_FLINK
, &flink
))
2290 pthread_mutex_lock(&bufmgr
->lock
);
2291 if (!bo_gem
->global_name
) {
2292 bo_gem
->global_name
= flink
.name
;
2293 bo_gem
->reusable
= false;
2295 HASH_ADD(name_hh
, bufmgr
->name_table
,
2296 global_name
, sizeof(bo_gem
->global_name
),
2299 pthread_mutex_unlock(&bufmgr
->lock
);
2302 *name
= bo_gem
->global_name
;
2307 * Enables unlimited caching of buffer objects for reuse.
2309 * This is potentially very memory expensive, as the cache at each bucket
2310 * size is only bounded by how many buffers of that size we've managed to have
2311 * in flight at once.
2314 drm_bacon_bufmgr_gem_enable_reuse(drm_bacon_bufmgr
*bufmgr
)
2316 bufmgr
->bo_reuse
= true;
2320 * Disables implicit synchronisation before executing the bo
2322 * This will cause rendering corruption unless you correctly manage explicit
2323 * fences for all rendering involving this buffer - including use by others.
2324 * Disabling the implicit serialisation is only required if that serialisation
2325 * is too coarse (for example, you have split the buffer into many
2326 * non-overlapping regions and are sharing the whole buffer between concurrent
2327 * independent command streams).
2329 * Note the kernel must advertise support via I915_PARAM_HAS_EXEC_ASYNC,
2330 * which can be checked using drm_bacon_bufmgr_can_disable_implicit_sync,
2331 * or subsequent execbufs involving the bo will generate EINVAL.
2334 drm_bacon_gem_bo_disable_implicit_sync(drm_bacon_bo
*bo
)
2336 drm_bacon_bo_gem
*bo_gem
= (drm_bacon_bo_gem
*) bo
;
2338 bo_gem
->kflags
|= EXEC_OBJECT_ASYNC
;
2342 * Enables implicit synchronisation before executing the bo
2344 * This is the default behaviour of the kernel, to wait upon prior writes
2345 * completing on the object before rendering with it, or to wait for prior
2346 * reads to complete before writing into the object.
2347 * drm_bacon_gem_bo_disable_implicit_sync() can stop this behaviour, telling
2348 * the kernel never to insert a stall before using the object. Then this
2349 * function can be used to restore the implicit sync before subsequent
2353 drm_bacon_gem_bo_enable_implicit_sync(drm_bacon_bo
*bo
)
2355 drm_bacon_bo_gem
*bo_gem
= (drm_bacon_bo_gem
*) bo
;
2357 bo_gem
->kflags
&= ~EXEC_OBJECT_ASYNC
;
2361 * Query whether the kernel supports disabling of its implicit synchronisation
2362 * before execbuf. See drm_bacon_gem_bo_disable_implicit_sync()
2365 drm_bacon_bufmgr_gem_can_disable_implicit_sync(drm_bacon_bufmgr
*bufmgr
)
2367 return bufmgr
->has_exec_async
;
2371 * Return the additional aperture space required by the tree of buffer objects
2375 drm_bacon_gem_bo_get_aperture_space(drm_bacon_bo
*bo
)
2377 drm_bacon_bo_gem
*bo_gem
= (drm_bacon_bo_gem
*) bo
;
2381 if (bo
== NULL
|| bo_gem
->included_in_check_aperture
)
2385 bo_gem
->included_in_check_aperture
= true;
2387 for (i
= 0; i
< bo_gem
->reloc_count
; i
++)
2389 drm_bacon_gem_bo_get_aperture_space(bo_gem
->
2390 reloc_target_info
[i
].bo
);
2396 * Clear the flag set by drm_bacon_gem_bo_get_aperture_space() so we're ready
2397 * for the next drm_bacon_bufmgr_check_aperture_space() call.
2400 drm_bacon_gem_bo_clear_aperture_space_flag(drm_bacon_bo
*bo
)
2402 drm_bacon_bo_gem
*bo_gem
= (drm_bacon_bo_gem
*) bo
;
2405 if (bo
== NULL
|| !bo_gem
->included_in_check_aperture
)
2408 bo_gem
->included_in_check_aperture
= false;
2410 for (i
= 0; i
< bo_gem
->reloc_count
; i
++)
2411 drm_bacon_gem_bo_clear_aperture_space_flag(bo_gem
->
2412 reloc_target_info
[i
].bo
);
2416 * Return a conservative estimate for the amount of aperture required
2417 * for a collection of buffers. This may double-count some buffers.
2420 drm_bacon_gem_estimate_batch_space(drm_bacon_bo
**bo_array
, int count
)
2423 unsigned int total
= 0;
2425 for (i
= 0; i
< count
; i
++) {
2426 drm_bacon_bo_gem
*bo_gem
= (drm_bacon_bo_gem
*) bo_array
[i
];
2428 total
+= bo_gem
->reloc_tree_size
;
2434 * Return the amount of aperture needed for a collection of buffers.
2435 * This avoids double counting any buffers, at the cost of looking
2436 * at every buffer in the set.
2439 drm_bacon_gem_compute_batch_space(drm_bacon_bo
**bo_array
, int count
)
2442 unsigned int total
= 0;
2444 for (i
= 0; i
< count
; i
++) {
2445 total
+= drm_bacon_gem_bo_get_aperture_space(bo_array
[i
]);
2446 /* For the first buffer object in the array, we get an
2447 * accurate count back for its reloc_tree size (since nothing
2448 * had been flagged as being counted yet). We can save that
2449 * value out as a more conservative reloc_tree_size that
2450 * avoids double-counting target buffers. Since the first
2451 * buffer happens to usually be the batch buffer in our
2452 * callers, this can pull us back from doing the tree
2453 * walk on every new batch emit.
2456 drm_bacon_bo_gem
*bo_gem
=
2457 (drm_bacon_bo_gem
*) bo_array
[i
];
2458 bo_gem
->reloc_tree_size
= total
;
2462 for (i
= 0; i
< count
; i
++)
2463 drm_bacon_gem_bo_clear_aperture_space_flag(bo_array
[i
]);
2468 * Return -1 if the batchbuffer should be flushed before attempting to
2469 * emit rendering referencing the buffers pointed to by bo_array.
2471 * This is required because if we try to emit a batchbuffer with relocations
2472 * to a tree of buffers that won't simultaneously fit in the aperture,
2473 * the rendering will return an error at a point where the software is not
2474 * prepared to recover from it.
2476 * However, we also want to emit the batchbuffer significantly before we reach
2477 * the limit, as a series of batchbuffers each of which references buffers
2478 * covering almost all of the aperture means that at each emit we end up
2479 * waiting to evict a buffer from the last rendering, and we get synchronous
2480 * performance. By emitting smaller batchbuffers, we eat some CPU overhead to
2481 * get better parallelism.
2484 drm_bacon_bufmgr_check_aperture_space(drm_bacon_bo
**bo_array
, int count
)
2486 drm_bacon_bufmgr
*bufmgr
= bo_array
[0]->bufmgr
;
2487 unsigned int total
= 0;
2488 unsigned int threshold
= bufmgr
->gtt_size
* 3 / 4;
2490 total
= drm_bacon_gem_estimate_batch_space(bo_array
, count
);
2492 if (total
> threshold
)
2493 total
= drm_bacon_gem_compute_batch_space(bo_array
, count
);
2495 if (total
> threshold
) {
2496 DBG("check_space: overflowed available aperture, "
2498 total
/ 1024, (int)bufmgr
->gtt_size
/ 1024);
2501 DBG("drm_check_space: total %dkb vs bufgr %dkb\n", total
/ 1024,
2502 (int)bufmgr
->gtt_size
/ 1024);
2508 * Disable buffer reuse for objects which are shared with the kernel
2509 * as scanout buffers
2512 drm_bacon_bo_disable_reuse(drm_bacon_bo
*bo
)
2514 drm_bacon_bo_gem
*bo_gem
= (drm_bacon_bo_gem
*) bo
;
2516 bo_gem
->reusable
= false;
2521 drm_bacon_bo_is_reusable(drm_bacon_bo
*bo
)
2523 drm_bacon_bo_gem
*bo_gem
= (drm_bacon_bo_gem
*) bo
;
2525 return bo_gem
->reusable
;
2529 _drm_bacon_gem_bo_references(drm_bacon_bo
*bo
, drm_bacon_bo
*target_bo
)
2531 drm_bacon_bo_gem
*bo_gem
= (drm_bacon_bo_gem
*) bo
;
2534 for (i
= 0; i
< bo_gem
->reloc_count
; i
++) {
2535 if (bo_gem
->reloc_target_info
[i
].bo
== target_bo
)
2537 if (bo
== bo_gem
->reloc_target_info
[i
].bo
)
2539 if (_drm_bacon_gem_bo_references(bo_gem
->reloc_target_info
[i
].bo
,
2544 for (i
= 0; i
< bo_gem
->softpin_target_count
; i
++) {
2545 if (bo_gem
->softpin_target
[i
] == target_bo
)
2547 if (_drm_bacon_gem_bo_references(bo_gem
->softpin_target
[i
], target_bo
))
2554 /** Return true if target_bo is referenced by bo's relocation tree. */
2556 drm_bacon_bo_references(drm_bacon_bo
*bo
, drm_bacon_bo
*target_bo
)
2558 drm_bacon_bo_gem
*target_bo_gem
= (drm_bacon_bo_gem
*) target_bo
;
2560 if (bo
== NULL
|| target_bo
== NULL
)
2562 if (target_bo_gem
->used_as_reloc_target
)
2563 return _drm_bacon_gem_bo_references(bo
, target_bo
);
2568 add_bucket(drm_bacon_bufmgr
*bufmgr
, int size
)
2570 unsigned int i
= bufmgr
->num_buckets
;
2572 assert(i
< ARRAY_SIZE(bufmgr
->cache_bucket
));
2574 list_inithead(&bufmgr
->cache_bucket
[i
].head
);
2575 bufmgr
->cache_bucket
[i
].size
= size
;
2576 bufmgr
->num_buckets
++;
2580 init_cache_buckets(drm_bacon_bufmgr
*bufmgr
)
2582 unsigned long size
, cache_max_size
= 64 * 1024 * 1024;
2584 /* OK, so power of two buckets was too wasteful of memory.
2585 * Give 3 other sizes between each power of two, to hopefully
2586 * cover things accurately enough. (The alternative is
2587 * probably to just go for exact matching of sizes, and assume
2588 * that for things like composited window resize the tiled
2589 * width/height alignment and rounding of sizes to pages will
2590 * get us useful cache hit rates anyway)
2592 add_bucket(bufmgr
, 4096);
2593 add_bucket(bufmgr
, 4096 * 2);
2594 add_bucket(bufmgr
, 4096 * 3);
2596 /* Initialize the linked lists for BO reuse cache. */
2597 for (size
= 4 * 4096; size
<= cache_max_size
; size
*= 2) {
2598 add_bucket(bufmgr
, size
);
2600 add_bucket(bufmgr
, size
+ size
* 1 / 4);
2601 add_bucket(bufmgr
, size
+ size
* 2 / 4);
2602 add_bucket(bufmgr
, size
+ size
* 3 / 4);
2607 drm_bacon_bufmgr_gem_set_vma_cache_size(drm_bacon_bufmgr
*bufmgr
, int limit
)
2609 bufmgr
->vma_max
= limit
;
2611 drm_bacon_gem_bo_purge_vma_cache(bufmgr
);
2615 drm_bacon_gem_context_create(drm_bacon_bufmgr
*bufmgr
)
2617 struct drm_i915_gem_context_create create
;
2618 drm_bacon_context
*context
= NULL
;
2621 context
= calloc(1, sizeof(*context
));
2626 ret
= drmIoctl(bufmgr
->fd
, DRM_IOCTL_I915_GEM_CONTEXT_CREATE
, &create
);
2628 DBG("DRM_IOCTL_I915_GEM_CONTEXT_CREATE failed: %s\n",
2634 context
->ctx_id
= create
.ctx_id
;
2635 context
->bufmgr
= bufmgr
;
2641 drm_bacon_gem_context_get_id(drm_bacon_context
*ctx
, uint32_t *ctx_id
)
2646 *ctx_id
= ctx
->ctx_id
;
2652 drm_bacon_gem_context_destroy(drm_bacon_context
*ctx
)
2654 struct drm_i915_gem_context_destroy destroy
;
2662 destroy
.ctx_id
= ctx
->ctx_id
;
2663 ret
= drmIoctl(ctx
->bufmgr
->fd
, DRM_IOCTL_I915_GEM_CONTEXT_DESTROY
,
2666 fprintf(stderr
, "DRM_IOCTL_I915_GEM_CONTEXT_DESTROY failed: %s\n",
2673 drm_bacon_get_reset_stats(drm_bacon_context
*ctx
,
2674 uint32_t *reset_count
,
2678 struct drm_i915_reset_stats stats
;
2686 stats
.ctx_id
= ctx
->ctx_id
;
2687 ret
= drmIoctl(ctx
->bufmgr
->fd
,
2688 DRM_IOCTL_I915_GET_RESET_STATS
,
2691 if (reset_count
!= NULL
)
2692 *reset_count
= stats
.reset_count
;
2695 *active
= stats
.batch_active
;
2697 if (pending
!= NULL
)
2698 *pending
= stats
.batch_pending
;
2705 drm_bacon_reg_read(drm_bacon_bufmgr
*bufmgr
,
2709 struct drm_i915_reg_read reg_read
;
2713 reg_read
.offset
= offset
;
2715 ret
= drmIoctl(bufmgr
->fd
, DRM_IOCTL_I915_REG_READ
, ®_read
);
2717 *result
= reg_read
.val
;
2721 static pthread_mutex_t bufmgr_list_mutex
= PTHREAD_MUTEX_INITIALIZER
;
2722 static struct list_head bufmgr_list
= { &bufmgr_list
, &bufmgr_list
};
2724 static drm_bacon_bufmgr
*
2725 drm_bacon_bufmgr_gem_find(int fd
)
2727 list_for_each_entry(drm_bacon_bufmgr
,
2728 bufmgr
, &bufmgr_list
, managers
) {
2729 if (bufmgr
->fd
== fd
) {
2730 p_atomic_inc(&bufmgr
->refcount
);
2739 drm_bacon_bufmgr_destroy(drm_bacon_bufmgr
*bufmgr
)
2741 if (atomic_add_unless(&bufmgr
->refcount
, -1, 1)) {
2742 pthread_mutex_lock(&bufmgr_list_mutex
);
2744 if (p_atomic_dec_zero(&bufmgr
->refcount
)) {
2745 list_del(&bufmgr
->managers
);
2746 drm_bacon_bufmgr_gem_destroy(bufmgr
);
2749 pthread_mutex_unlock(&bufmgr_list_mutex
);
2753 void *drm_bacon_gem_bo_map__gtt(drm_bacon_bo
*bo
)
2755 drm_bacon_bufmgr
*bufmgr
= bo
->bufmgr
;
2756 drm_bacon_bo_gem
*bo_gem
= (drm_bacon_bo_gem
*) bo
;
2758 if (bo_gem
->gtt_virtual
)
2759 return bo_gem
->gtt_virtual
;
2761 if (bo_gem
->is_userptr
)
2764 pthread_mutex_lock(&bufmgr
->lock
);
2765 if (bo_gem
->gtt_virtual
== NULL
) {
2766 struct drm_i915_gem_mmap_gtt mmap_arg
;
2769 DBG("bo_map_gtt: mmap %d (%s), map_count=%d\n",
2770 bo_gem
->gem_handle
, bo_gem
->name
, bo_gem
->map_count
);
2772 if (bo_gem
->map_count
++ == 0)
2773 drm_bacon_gem_bo_open_vma(bufmgr
, bo_gem
);
2776 mmap_arg
.handle
= bo_gem
->gem_handle
;
2778 /* Get the fake offset back... */
2780 if (drmIoctl(bufmgr
->fd
,
2781 DRM_IOCTL_I915_GEM_MMAP_GTT
,
2784 ptr
= drm_mmap(0, bo
->size
, PROT_READ
| PROT_WRITE
,
2785 MAP_SHARED
, bufmgr
->fd
,
2788 if (ptr
== MAP_FAILED
) {
2789 if (--bo_gem
->map_count
== 0)
2790 drm_bacon_gem_bo_close_vma(bufmgr
, bo_gem
);
2794 bo_gem
->gtt_virtual
= ptr
;
2796 pthread_mutex_unlock(&bufmgr
->lock
);
2798 return bo_gem
->gtt_virtual
;
2801 void *drm_bacon_gem_bo_map__cpu(drm_bacon_bo
*bo
)
2803 drm_bacon_bufmgr
*bufmgr
= bo
->bufmgr
;
2804 drm_bacon_bo_gem
*bo_gem
= (drm_bacon_bo_gem
*) bo
;
2806 if (bo_gem
->mem_virtual
)
2807 return bo_gem
->mem_virtual
;
2809 if (bo_gem
->is_userptr
) {
2810 /* Return the same user ptr */
2811 return bo_gem
->user_virtual
;
2814 pthread_mutex_lock(&bufmgr
->lock
);
2815 if (!bo_gem
->mem_virtual
) {
2816 struct drm_i915_gem_mmap mmap_arg
;
2818 if (bo_gem
->map_count
++ == 0)
2819 drm_bacon_gem_bo_open_vma(bufmgr
, bo_gem
);
2821 DBG("bo_map: %d (%s), map_count=%d\n",
2822 bo_gem
->gem_handle
, bo_gem
->name
, bo_gem
->map_count
);
2825 mmap_arg
.handle
= bo_gem
->gem_handle
;
2826 mmap_arg
.size
= bo
->size
;
2827 if (drmIoctl(bufmgr
->fd
,
2828 DRM_IOCTL_I915_GEM_MMAP
,
2830 DBG("%s:%d: Error mapping buffer %d (%s): %s .\n",
2831 __FILE__
, __LINE__
, bo_gem
->gem_handle
,
2832 bo_gem
->name
, strerror(errno
));
2833 if (--bo_gem
->map_count
== 0)
2834 drm_bacon_gem_bo_close_vma(bufmgr
, bo_gem
);
2836 VG(VALGRIND_MALLOCLIKE_BLOCK(mmap_arg
.addr_ptr
, mmap_arg
.size
, 0, 1));
2837 bo_gem
->mem_virtual
= (void *)(uintptr_t) mmap_arg
.addr_ptr
;
2840 pthread_mutex_unlock(&bufmgr
->lock
);
2842 return bo_gem
->mem_virtual
;
2845 void *drm_bacon_gem_bo_map__wc(drm_bacon_bo
*bo
)
2847 drm_bacon_bufmgr
*bufmgr
= bo
->bufmgr
;
2848 drm_bacon_bo_gem
*bo_gem
= (drm_bacon_bo_gem
*) bo
;
2850 if (bo_gem
->wc_virtual
)
2851 return bo_gem
->wc_virtual
;
2853 if (bo_gem
->is_userptr
)
2856 pthread_mutex_lock(&bufmgr
->lock
);
2857 if (!bo_gem
->wc_virtual
) {
2858 struct drm_i915_gem_mmap mmap_arg
;
2860 if (bo_gem
->map_count
++ == 0)
2861 drm_bacon_gem_bo_open_vma(bufmgr
, bo_gem
);
2863 DBG("bo_map: %d (%s), map_count=%d\n",
2864 bo_gem
->gem_handle
, bo_gem
->name
, bo_gem
->map_count
);
2867 mmap_arg
.handle
= bo_gem
->gem_handle
;
2868 mmap_arg
.size
= bo
->size
;
2869 mmap_arg
.flags
= I915_MMAP_WC
;
2870 if (drmIoctl(bufmgr
->fd
,
2871 DRM_IOCTL_I915_GEM_MMAP
,
2873 DBG("%s:%d: Error mapping buffer %d (%s): %s .\n",
2874 __FILE__
, __LINE__
, bo_gem
->gem_handle
,
2875 bo_gem
->name
, strerror(errno
));
2876 if (--bo_gem
->map_count
== 0)
2877 drm_bacon_gem_bo_close_vma(bufmgr
, bo_gem
);
2879 VG(VALGRIND_MALLOCLIKE_BLOCK(mmap_arg
.addr_ptr
, mmap_arg
.size
, 0, 1));
2880 bo_gem
->wc_virtual
= (void *)(uintptr_t) mmap_arg
.addr_ptr
;
2883 pthread_mutex_unlock(&bufmgr
->lock
);
2885 return bo_gem
->wc_virtual
;
2889 * Initializes the GEM buffer manager, which uses the kernel to allocate, map,
2890 * and manage map buffer objections.
2892 * \param fd File descriptor of the opened DRM device.
2895 drm_bacon_bufmgr_gem_init(struct gen_device_info
*devinfo
,
2896 int fd
, int batch_size
)
2898 drm_bacon_bufmgr
*bufmgr
;
2899 struct drm_i915_gem_get_aperture aperture
;
2900 drm_i915_getparam_t gp
;
2903 pthread_mutex_lock(&bufmgr_list_mutex
);
2905 bufmgr
= drm_bacon_bufmgr_gem_find(fd
);
2909 bufmgr
= calloc(1, sizeof(*bufmgr
));
2914 p_atomic_set(&bufmgr
->refcount
, 1);
2916 if (pthread_mutex_init(&bufmgr
->lock
, NULL
) != 0) {
2923 drmIoctl(bufmgr
->fd
, DRM_IOCTL_I915_GEM_GET_APERTURE
, &aperture
);
2924 bufmgr
->gtt_size
= aperture
.aper_available_size
;
2929 gp
.param
= I915_PARAM_HAS_EXEC_ASYNC
;
2930 ret
= drmIoctl(bufmgr
->fd
, DRM_IOCTL_I915_GETPARAM
, &gp
);
2931 bufmgr
->has_exec_async
= ret
== 0;
2933 bufmgr
->has_llc
= devinfo
->has_llc
;
2935 /* Let's go with one relocation per every 2 dwords (but round down a bit
2936 * since a power of two will mean an extra page allocation for the reloc
2939 * Every 4 was too few for the blender benchmark.
2941 bufmgr
->max_relocs
= batch_size
/ sizeof(uint32_t) / 2 - 2;
2943 init_cache_buckets(bufmgr
);
2945 list_inithead(&bufmgr
->vma_cache
);
2946 bufmgr
->vma_max
= -1; /* unlimited by default */
2948 list_add(&bufmgr
->managers
, &bufmgr_list
);
2951 pthread_mutex_unlock(&bufmgr_list_mutex
);