1 /**************************************************************************
3 * Copyright © 2007 Red Hat Inc.
4 * Copyright © 2007 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>
49 #include <sys/ioctl.h>
52 #include <sys/types.h>
55 #include "libdrm_lists.h"
56 #include "intel_bufmgr.h"
57 #include "intel_bufmgr_priv.h"
58 #include "intel_chipset.h"
63 #define DBG(...) do { \
64 if (bufmgr_gem->bufmgr.debug) \
65 fprintf(stderr, __VA_ARGS__); \
68 typedef struct _drm_intel_bo_gem drm_intel_bo_gem
;
70 struct drm_intel_gem_bo_bucket
{
74 * Limit on the number of entries in this bucket.
76 * 0 means that this caching at this bucket size is disabled.
77 * -1 means that there is no limit to caching at this size.
83 /* Only cache objects up to 64MB. Bigger than that, and the rounding of the
84 * size makes many operations fail that wouldn't otherwise.
86 #define DRM_INTEL_GEM_BO_BUCKETS 14
87 typedef struct _drm_intel_bufmgr_gem
{
88 drm_intel_bufmgr bufmgr
;
96 struct drm_i915_gem_exec_object
*exec_objects
;
97 drm_intel_bo
**exec_bos
;
101 /** Array of lists of cached gem objects of power-of-two sizes */
102 struct drm_intel_gem_bo_bucket cache_bucket
[DRM_INTEL_GEM_BO_BUCKETS
];
105 int available_fences
;
107 } drm_intel_bufmgr_gem
;
109 struct _drm_intel_bo_gem
{
113 /** Boolean whether the mmap ioctl has been called for this buffer yet. */
118 * Kenel-assigned global name for this object
120 unsigned int global_name
;
123 * Index of the buffer within the validation list while preparing a
124 * batchbuffer execution.
129 * Boolean whether we've started swrast
130 * Set when the buffer has been mapped
131 * Cleared when the buffer is unmapped
136 * Current tiling mode
138 uint32_t tiling_mode
;
139 uint32_t swizzle_mode
;
141 /** Array passed to the DRM containing relocation information. */
142 struct drm_i915_gem_relocation_entry
*relocs
;
143 /** Array of bos corresponding to relocs[i].target_handle */
144 drm_intel_bo
**reloc_target_bo
;
145 /** Number of entries in relocs */
147 /** Mapped address for the buffer, saved across map/unmap cycles */
149 /** GTT virtual address for the buffer, saved across map/unmap cycles */
156 * Boolean of whether this BO and its children have been included in
157 * the current drm_intel_bufmgr_check_aperture_space() total.
159 char included_in_check_aperture
;
162 * Boolean of whether this buffer has been used as a relocation
163 * target and had its size accounted for, and thus can't have any
164 * further relocations added to it.
166 char used_as_reloc_target
;
169 * Boolean of whether this buffer can be re-used
174 * Size in bytes of this buffer and its relocation descendents.
176 * Used to avoid costly tree walking in drm_intel_bufmgr_check_aperture in
181 * Number of potential fence registers required by this buffer and its
184 int reloc_tree_fences
;
187 static void drm_intel_gem_bo_reference_locked(drm_intel_bo
*bo
);
190 drm_intel_gem_estimate_batch_space(drm_intel_bo
**bo_array
, int count
);
193 drm_intel_gem_compute_batch_space(drm_intel_bo
**bo_array
, int count
);
196 drm_intel_gem_bo_get_tiling(drm_intel_bo
*bo
, uint32_t *tiling_mode
,
197 uint32_t *swizzle_mode
);
200 drm_intel_gem_bo_set_tiling(drm_intel_bo
*bo
, uint32_t *tiling_mode
,
204 drm_intel_gem_bo_unreference(drm_intel_bo
*bo
);
220 static struct drm_intel_gem_bo_bucket
*
221 drm_intel_gem_bo_bucket_for_size(drm_intel_bufmgr_gem
*bufmgr_gem
,
226 /* We only do buckets in power of two increments */
227 if ((size
& (size
- 1)) != 0)
230 /* We should only see sizes rounded to pages. */
231 assert((size
% 4096) == 0);
233 /* We always allocate in units of pages */
234 i
= ffs(size
/ 4096) - 1;
235 if (i
>= DRM_INTEL_GEM_BO_BUCKETS
)
238 return &bufmgr_gem
->cache_bucket
[i
];
242 static void drm_intel_gem_dump_validation_list(drm_intel_bufmgr_gem
*bufmgr_gem
)
246 for (i
= 0; i
< bufmgr_gem
->exec_count
; i
++) {
247 drm_intel_bo
*bo
= bufmgr_gem
->exec_bos
[i
];
248 drm_intel_bo_gem
*bo_gem
= (drm_intel_bo_gem
*)bo
;
250 if (bo_gem
->relocs
== NULL
) {
251 DBG("%2d: %d (%s)\n", i
, bo_gem
->gem_handle
, bo_gem
->name
);
255 for (j
= 0; j
< bo_gem
->reloc_count
; j
++) {
256 drm_intel_bo
*target_bo
= bo_gem
->reloc_target_bo
[j
];
257 drm_intel_bo_gem
*target_gem
= (drm_intel_bo_gem
*)target_bo
;
259 DBG("%2d: %d (%s)@0x%08llx -> %d (%s)@0x%08lx + 0x%08x\n",
261 bo_gem
->gem_handle
, bo_gem
->name
,
262 (unsigned long long)bo_gem
->relocs
[j
].offset
,
263 target_gem
->gem_handle
, target_gem
->name
, target_bo
->offset
,
264 bo_gem
->relocs
[j
].delta
);
270 * Adds the given buffer to the list of buffers to be validated (moved into the
271 * appropriate memory type) with the next batch submission.
273 * If a buffer is validated multiple times in a batch submission, it ends up
274 * with the intersection of the memory type flags and the union of the
278 drm_intel_add_validate_buffer(drm_intel_bo
*bo
)
280 drm_intel_bufmgr_gem
*bufmgr_gem
= (drm_intel_bufmgr_gem
*)bo
->bufmgr
;
281 drm_intel_bo_gem
*bo_gem
= (drm_intel_bo_gem
*)bo
;
284 if (bo_gem
->validate_index
!= -1)
287 /* Extend the array of validation entries as necessary. */
288 if (bufmgr_gem
->exec_count
== bufmgr_gem
->exec_size
) {
289 int new_size
= bufmgr_gem
->exec_size
* 2;
294 bufmgr_gem
->exec_objects
=
295 realloc(bufmgr_gem
->exec_objects
,
296 sizeof(*bufmgr_gem
->exec_objects
) * new_size
);
297 bufmgr_gem
->exec_bos
=
298 realloc(bufmgr_gem
->exec_bos
,
299 sizeof(*bufmgr_gem
->exec_bos
) * new_size
);
300 bufmgr_gem
->exec_size
= new_size
;
303 index
= bufmgr_gem
->exec_count
;
304 bo_gem
->validate_index
= index
;
305 /* Fill in array entry */
306 bufmgr_gem
->exec_objects
[index
].handle
= bo_gem
->gem_handle
;
307 bufmgr_gem
->exec_objects
[index
].relocation_count
= bo_gem
->reloc_count
;
308 bufmgr_gem
->exec_objects
[index
].relocs_ptr
= (uintptr_t)bo_gem
->relocs
;
309 bufmgr_gem
->exec_objects
[index
].alignment
= 0;
310 bufmgr_gem
->exec_objects
[index
].offset
= 0;
311 bufmgr_gem
->exec_bos
[index
] = bo
;
312 drm_intel_gem_bo_reference_locked(bo
);
313 bufmgr_gem
->exec_count
++;
317 #define RELOC_BUF_SIZE(x) ((I915_RELOC_HEADER + x * I915_RELOC0_STRIDE) * \
321 drm_intel_setup_reloc_list(drm_intel_bo
*bo
)
323 drm_intel_bo_gem
*bo_gem
= (drm_intel_bo_gem
*)bo
;
324 drm_intel_bufmgr_gem
*bufmgr_gem
= (drm_intel_bufmgr_gem
*)bo
->bufmgr
;
326 bo_gem
->relocs
= malloc(bufmgr_gem
->max_relocs
*
327 sizeof(struct drm_i915_gem_relocation_entry
));
328 bo_gem
->reloc_target_bo
= malloc(bufmgr_gem
->max_relocs
*
329 sizeof(drm_intel_bo
*));
334 static drm_intel_bo
*
335 drm_intel_gem_bo_alloc_internal(drm_intel_bufmgr
*bufmgr
, const char *name
,
336 unsigned long size
, unsigned int alignment
,
339 drm_intel_bufmgr_gem
*bufmgr_gem
= (drm_intel_bufmgr_gem
*)bufmgr
;
340 drm_intel_bo_gem
*bo_gem
;
341 unsigned int page_size
= getpagesize();
343 struct drm_intel_gem_bo_bucket
*bucket
;
344 int alloc_from_cache
= 0;
345 unsigned long bo_size
;
347 /* Round the allocated size up to a power of two number of pages. */
348 bo_size
= 1 << logbase2(size
);
349 if (bo_size
< page_size
)
351 bucket
= drm_intel_gem_bo_bucket_for_size(bufmgr_gem
, bo_size
);
353 /* If we don't have caching at this size, don't actually round the
356 if (bucket
== NULL
|| bucket
->max_entries
== 0) {
358 if (bo_size
< page_size
)
362 pthread_mutex_lock(&bufmgr_gem
->lock
);
363 /* Get a buffer out of the cache if available */
364 if (bucket
!= NULL
&& bucket
->num_entries
> 0) {
365 struct drm_i915_gem_busy busy
;
368 /* Allocate new render-target BOs from the tail (MRU)
369 * of the list, as it will likely be hot in the GPU cache
370 * and in the aperture for us.
372 bo_gem
= DRMLISTENTRY(drm_intel_bo_gem
, bucket
->head
.prev
, head
);
373 DRMLISTDEL(&bo_gem
->head
);
374 bucket
->num_entries
--;
375 alloc_from_cache
= 1;
377 /* For non-render-target BOs (where we're probably going to map it
378 * first thing in order to fill it with data), check if the
379 * last BO in the cache is unbusy, and only reuse in that case.
380 * Otherwise, allocating a new buffer is probably faster than
381 * waiting for the GPU to finish.
383 bo_gem
= DRMLISTENTRY(drm_intel_bo_gem
, bucket
->head
.next
, head
);
385 memset(&busy
, 0, sizeof(busy
));
386 busy
.handle
= bo_gem
->gem_handle
;
388 ret
= ioctl(bufmgr_gem
->fd
, DRM_IOCTL_I915_GEM_BUSY
, &busy
);
389 alloc_from_cache
= (ret
== 0 && busy
.busy
== 0);
391 if (alloc_from_cache
) {
392 DRMLISTDEL(&bo_gem
->head
);
393 bucket
->num_entries
--;
397 pthread_mutex_unlock(&bufmgr_gem
->lock
);
399 if (!alloc_from_cache
) {
400 struct drm_i915_gem_create create
;
402 bo_gem
= calloc(1, sizeof(*bo_gem
));
406 bo_gem
->bo
.size
= bo_size
;
407 memset(&create
, 0, sizeof(create
));
408 create
.size
= bo_size
;
410 ret
= ioctl(bufmgr_gem
->fd
, DRM_IOCTL_I915_GEM_CREATE
, &create
);
411 bo_gem
->gem_handle
= create
.handle
;
412 bo_gem
->bo
.handle
= bo_gem
->gem_handle
;
417 bo_gem
->bo
.bufmgr
= bufmgr
;
421 bo_gem
->refcount
= 1;
422 bo_gem
->validate_index
= -1;
423 bo_gem
->reloc_tree_size
= bo_gem
->bo
.size
;
424 bo_gem
->reloc_tree_fences
= 0;
425 bo_gem
->used_as_reloc_target
= 0;
426 bo_gem
->tiling_mode
= I915_TILING_NONE
;
427 bo_gem
->swizzle_mode
= I915_BIT_6_SWIZZLE_NONE
;
428 bo_gem
->reusable
= 1;
430 DBG("bo_create: buf %d (%s) %ldb\n",
431 bo_gem
->gem_handle
, bo_gem
->name
, size
);
436 static drm_intel_bo
*
437 drm_intel_gem_bo_alloc_for_render(drm_intel_bufmgr
*bufmgr
, const char *name
,
438 unsigned long size
, unsigned int alignment
)
440 return drm_intel_gem_bo_alloc_internal(bufmgr
, name
, size
, alignment
, 1);
443 static drm_intel_bo
*
444 drm_intel_gem_bo_alloc(drm_intel_bufmgr
*bufmgr
, const char *name
,
445 unsigned long size
, unsigned int alignment
)
447 return drm_intel_gem_bo_alloc_internal(bufmgr
, name
, size
, alignment
, 0);
451 * Returns a drm_intel_bo wrapping the given buffer object handle.
453 * This can be used when one application needs to pass a buffer object
457 drm_intel_bo_gem_create_from_name(drm_intel_bufmgr
*bufmgr
, const char *name
,
460 drm_intel_bufmgr_gem
*bufmgr_gem
= (drm_intel_bufmgr_gem
*)bufmgr
;
461 drm_intel_bo_gem
*bo_gem
;
463 struct drm_gem_open open_arg
;
464 struct drm_i915_gem_get_tiling get_tiling
;
466 bo_gem
= calloc(1, sizeof(*bo_gem
));
470 memset(&open_arg
, 0, sizeof(open_arg
));
471 open_arg
.name
= handle
;
472 ret
= ioctl(bufmgr_gem
->fd
, DRM_IOCTL_GEM_OPEN
, &open_arg
);
474 fprintf(stderr
, "Couldn't reference %s handle 0x%08x: %s\n",
475 name
, handle
, strerror(errno
));
479 bo_gem
->bo
.size
= open_arg
.size
;
480 bo_gem
->bo
.offset
= 0;
481 bo_gem
->bo
.virtual = NULL
;
482 bo_gem
->bo
.bufmgr
= bufmgr
;
484 bo_gem
->refcount
= 1;
485 bo_gem
->validate_index
= -1;
486 bo_gem
->gem_handle
= open_arg
.handle
;
487 bo_gem
->global_name
= handle
;
488 bo_gem
->reusable
= 0;
490 memset(&get_tiling
, 0, sizeof(get_tiling
));
491 get_tiling
.handle
= bo_gem
->gem_handle
;
492 ret
= ioctl(bufmgr_gem
->fd
, DRM_IOCTL_I915_GEM_GET_TILING
, &get_tiling
);
494 drm_intel_gem_bo_unreference(&bo_gem
->bo
);
497 bo_gem
->tiling_mode
= get_tiling
.tiling_mode
;
498 bo_gem
->swizzle_mode
= get_tiling
.swizzle_mode
;
499 if (bo_gem
->tiling_mode
== I915_TILING_NONE
)
500 bo_gem
->reloc_tree_fences
= 0;
502 bo_gem
->reloc_tree_fences
= 1;
504 DBG("bo_create_from_handle: %d (%s)\n", handle
, bo_gem
->name
);
510 drm_intel_gem_bo_reference(drm_intel_bo
*bo
)
512 drm_intel_bufmgr_gem
*bufmgr_gem
= (drm_intel_bufmgr_gem
*)bo
->bufmgr
;
513 drm_intel_bo_gem
*bo_gem
= (drm_intel_bo_gem
*)bo
;
515 assert(bo_gem
->refcount
> 0);
516 pthread_mutex_lock(&bufmgr_gem
->lock
);
518 pthread_mutex_unlock(&bufmgr_gem
->lock
);
522 drm_intel_gem_bo_reference_locked(drm_intel_bo
*bo
)
524 drm_intel_bo_gem
*bo_gem
= (drm_intel_bo_gem
*)bo
;
526 assert(bo_gem
->refcount
> 0);
531 drm_intel_gem_bo_free(drm_intel_bo
*bo
)
533 drm_intel_bufmgr_gem
*bufmgr_gem
= (drm_intel_bufmgr_gem
*)bo
->bufmgr
;
534 drm_intel_bo_gem
*bo_gem
= (drm_intel_bo_gem
*)bo
;
535 struct drm_gem_close close
;
538 if (bo_gem
->mem_virtual
)
539 munmap (bo_gem
->mem_virtual
, bo_gem
->bo
.size
);
540 if (bo_gem
->gtt_virtual
)
541 munmap (bo_gem
->gtt_virtual
, bo_gem
->bo
.size
);
543 /* Close this object */
544 memset(&close
, 0, sizeof(close
));
545 close
.handle
= bo_gem
->gem_handle
;
546 ret
= ioctl(bufmgr_gem
->fd
, DRM_IOCTL_GEM_CLOSE
, &close
);
549 "DRM_IOCTL_GEM_CLOSE %d failed (%s): %s\n",
550 bo_gem
->gem_handle
, bo_gem
->name
, strerror(errno
));
556 drm_intel_gem_bo_unreference_locked(drm_intel_bo
*bo
)
558 drm_intel_bufmgr_gem
*bufmgr_gem
= (drm_intel_bufmgr_gem
*)bo
->bufmgr
;
559 drm_intel_bo_gem
*bo_gem
= (drm_intel_bo_gem
*)bo
;
561 assert(bo_gem
->refcount
> 0);
562 if (--bo_gem
->refcount
== 0) {
563 struct drm_intel_gem_bo_bucket
*bucket
;
564 uint32_t tiling_mode
;
566 if (bo_gem
->relocs
!= NULL
) {
569 /* Unreference all the target buffers */
570 for (i
= 0; i
< bo_gem
->reloc_count
; i
++)
571 drm_intel_gem_bo_unreference_locked(bo_gem
->reloc_target_bo
[i
]);
572 free(bo_gem
->reloc_target_bo
);
573 free(bo_gem
->relocs
);
576 DBG("bo_unreference final: %d (%s)\n",
577 bo_gem
->gem_handle
, bo_gem
->name
);
579 bucket
= drm_intel_gem_bo_bucket_for_size(bufmgr_gem
, bo
->size
);
580 /* Put the buffer into our internal cache for reuse if we can. */
581 tiling_mode
= I915_TILING_NONE
;
582 if (bo_gem
->reusable
&&
584 (bucket
->max_entries
== -1 ||
585 (bucket
->max_entries
> 0 &&
586 bucket
->num_entries
< bucket
->max_entries
)) &&
587 drm_intel_gem_bo_set_tiling(bo
, &tiling_mode
, 0) == 0)
590 bo_gem
->validate_index
= -1;
591 bo_gem
->relocs
= NULL
;
592 bo_gem
->reloc_target_bo
= NULL
;
593 bo_gem
->reloc_count
= 0;
595 DRMLISTADDTAIL(&bo_gem
->head
, &bucket
->head
);
596 bucket
->num_entries
++;
598 drm_intel_gem_bo_free(bo
);
604 drm_intel_gem_bo_unreference(drm_intel_bo
*bo
)
606 drm_intel_bufmgr_gem
*bufmgr_gem
= (drm_intel_bufmgr_gem
*)bo
->bufmgr
;
608 pthread_mutex_lock(&bufmgr_gem
->lock
);
609 drm_intel_gem_bo_unreference_locked(bo
);
610 pthread_mutex_unlock(&bufmgr_gem
->lock
);
614 drm_intel_gem_bo_map(drm_intel_bo
*bo
, int write_enable
)
616 drm_intel_bufmgr_gem
*bufmgr_gem
= (drm_intel_bufmgr_gem
*)bo
->bufmgr
;
617 drm_intel_bo_gem
*bo_gem
= (drm_intel_bo_gem
*)bo
;
618 struct drm_i915_gem_set_domain set_domain
;
621 pthread_mutex_lock(&bufmgr_gem
->lock
);
623 /* Allow recursive mapping. Mesa may recursively map buffers with
624 * nested display loops.
626 if (!bo_gem
->mem_virtual
) {
627 struct drm_i915_gem_mmap mmap_arg
;
629 DBG("bo_map: %d (%s)\n", bo_gem
->gem_handle
, bo_gem
->name
);
631 memset(&mmap_arg
, 0, sizeof(mmap_arg
));
632 mmap_arg
.handle
= bo_gem
->gem_handle
;
634 mmap_arg
.size
= bo
->size
;
635 ret
= ioctl(bufmgr_gem
->fd
, DRM_IOCTL_I915_GEM_MMAP
, &mmap_arg
);
637 fprintf(stderr
, "%s:%d: Error mapping buffer %d (%s): %s .\n",
639 bo_gem
->gem_handle
, bo_gem
->name
, strerror(errno
));
640 pthread_mutex_unlock(&bufmgr_gem
->lock
);
643 bo_gem
->mem_virtual
= (void *)(uintptr_t)mmap_arg
.addr_ptr
;
646 DBG("bo_map: %d (%s) -> %p\n", bo_gem
->gem_handle
, bo_gem
->name
,
647 bo_gem
->mem_virtual
);
648 bo
->virtual = bo_gem
->mem_virtual
;
650 if (bo_gem
->global_name
!= 0 || !bo_gem
->swrast
) {
651 set_domain
.handle
= bo_gem
->gem_handle
;
652 set_domain
.read_domains
= I915_GEM_DOMAIN_CPU
;
654 set_domain
.write_domain
= I915_GEM_DOMAIN_CPU
;
656 set_domain
.write_domain
= 0;
658 ret
= ioctl(bufmgr_gem
->fd
, DRM_IOCTL_I915_GEM_SET_DOMAIN
,
660 } while (ret
== -1 && errno
== EINTR
);
662 fprintf (stderr
, "%s:%d: Error setting swrast %d: %s\n",
663 __FILE__
, __LINE__
, bo_gem
->gem_handle
, strerror (errno
));
664 pthread_mutex_unlock(&bufmgr_gem
->lock
);
670 pthread_mutex_unlock(&bufmgr_gem
->lock
);
676 drm_intel_gem_bo_map_gtt(drm_intel_bo
*bo
)
678 drm_intel_bufmgr_gem
*bufmgr_gem
= (drm_intel_bufmgr_gem
*)bo
->bufmgr
;
679 drm_intel_bo_gem
*bo_gem
= (drm_intel_bo_gem
*)bo
;
680 struct drm_i915_gem_set_domain set_domain
;
683 pthread_mutex_lock(&bufmgr_gem
->lock
);
685 /* Get a mapping of the buffer if we haven't before. */
686 if (bo_gem
->gtt_virtual
== NULL
) {
687 struct drm_i915_gem_mmap_gtt mmap_arg
;
689 DBG("bo_map_gtt: %d (%s)\n", bo_gem
->gem_handle
, bo_gem
->name
);
691 memset(&mmap_arg
, 0, sizeof(mmap_arg
));
692 mmap_arg
.handle
= bo_gem
->gem_handle
;
694 /* Get the fake offset back... */
695 ret
= ioctl(bufmgr_gem
->fd
, DRM_IOCTL_I915_GEM_MMAP_GTT
, &mmap_arg
);
698 "%s:%d: Error preparing buffer map %d (%s): %s .\n",
700 bo_gem
->gem_handle
, bo_gem
->name
,
702 pthread_mutex_unlock(&bufmgr_gem
->lock
);
707 bo_gem
->gtt_virtual
= mmap(0, bo
->size
, PROT_READ
| PROT_WRITE
,
708 MAP_SHARED
, bufmgr_gem
->fd
,
710 if (bo_gem
->gtt_virtual
== MAP_FAILED
) {
712 "%s:%d: Error mapping buffer %d (%s): %s .\n",
714 bo_gem
->gem_handle
, bo_gem
->name
,
716 pthread_mutex_unlock(&bufmgr_gem
->lock
);
721 bo
->virtual = bo_gem
->gtt_virtual
;
723 DBG("bo_map: %d (%s) -> %p\n", bo_gem
->gem_handle
, bo_gem
->name
,
724 bo_gem
->gtt_virtual
);
726 /* Now move it to the GTT domain so that the CPU caches are flushed */
727 set_domain
.handle
= bo_gem
->gem_handle
;
728 set_domain
.read_domains
= I915_GEM_DOMAIN_GTT
;
729 set_domain
.write_domain
= I915_GEM_DOMAIN_GTT
;
731 ret
= ioctl(bufmgr_gem
->fd
, DRM_IOCTL_I915_GEM_SET_DOMAIN
,
733 } while (ret
== -1 && errno
== EINTR
);
736 fprintf (stderr
, "%s:%d: Error setting domain %d: %s\n",
737 __FILE__
, __LINE__
, bo_gem
->gem_handle
, strerror (errno
));
740 pthread_mutex_unlock(&bufmgr_gem
->lock
);
746 drm_intel_gem_bo_unmap_gtt(drm_intel_bo
*bo
)
748 drm_intel_bufmgr_gem
*bufmgr_gem
= (drm_intel_bufmgr_gem
*)bo
->bufmgr
;
749 drm_intel_bo_gem
*bo_gem
= (drm_intel_bo_gem
*)bo
;
755 assert(bo_gem
->gtt_virtual
!= NULL
);
757 pthread_mutex_lock(&bufmgr_gem
->lock
);
759 pthread_mutex_unlock(&bufmgr_gem
->lock
);
765 drm_intel_gem_bo_unmap(drm_intel_bo
*bo
)
767 drm_intel_bufmgr_gem
*bufmgr_gem
= (drm_intel_bufmgr_gem
*)bo
->bufmgr
;
768 drm_intel_bo_gem
*bo_gem
= (drm_intel_bo_gem
*)bo
;
769 struct drm_i915_gem_sw_finish sw_finish
;
775 assert(bo_gem
->mem_virtual
!= NULL
);
777 pthread_mutex_lock(&bufmgr_gem
->lock
);
778 if (bo_gem
->swrast
) {
779 sw_finish
.handle
= bo_gem
->gem_handle
;
781 ret
= ioctl(bufmgr_gem
->fd
, DRM_IOCTL_I915_GEM_SW_FINISH
,
783 } while (ret
== -1 && errno
== EINTR
);
787 pthread_mutex_unlock(&bufmgr_gem
->lock
);
792 drm_intel_gem_bo_subdata (drm_intel_bo
*bo
, unsigned long offset
,
793 unsigned long size
, const void *data
)
795 drm_intel_bufmgr_gem
*bufmgr_gem
= (drm_intel_bufmgr_gem
*)bo
->bufmgr
;
796 drm_intel_bo_gem
*bo_gem
= (drm_intel_bo_gem
*)bo
;
797 struct drm_i915_gem_pwrite pwrite
;
800 memset (&pwrite
, 0, sizeof (pwrite
));
801 pwrite
.handle
= bo_gem
->gem_handle
;
802 pwrite
.offset
= offset
;
804 pwrite
.data_ptr
= (uint64_t) (uintptr_t) data
;
806 ret
= ioctl (bufmgr_gem
->fd
, DRM_IOCTL_I915_GEM_PWRITE
, &pwrite
);
807 } while (ret
== -1 && errno
== EINTR
);
809 fprintf (stderr
, "%s:%d: Error writing data to buffer %d: (%d %d) %s .\n",
811 bo_gem
->gem_handle
, (int) offset
, (int) size
,
818 drm_intel_gem_get_pipe_from_crtc_id (drm_intel_bufmgr
*bufmgr
, int crtc_id
)
820 drm_intel_bufmgr_gem
*bufmgr_gem
= (drm_intel_bufmgr_gem
*) bufmgr
;
821 struct drm_i915_get_pipe_from_crtc_id get_pipe_from_crtc_id
;
824 get_pipe_from_crtc_id
.crtc_id
= crtc_id
;
825 ret
= ioctl (bufmgr_gem
->fd
, DRM_IOCTL_I915_GET_PIPE_FROM_CRTC_ID
,
826 &get_pipe_from_crtc_id
);
828 /* We return -1 here to signal that we don't
829 * know which pipe is associated with this crtc.
830 * This lets the caller know that this information
831 * isn't available; using the wrong pipe for
832 * vblank waiting can cause the chipset to lock up
837 return get_pipe_from_crtc_id
.pipe
;
841 drm_intel_gem_bo_get_subdata (drm_intel_bo
*bo
, unsigned long offset
,
842 unsigned long size
, void *data
)
844 drm_intel_bufmgr_gem
*bufmgr_gem
= (drm_intel_bufmgr_gem
*)bo
->bufmgr
;
845 drm_intel_bo_gem
*bo_gem
= (drm_intel_bo_gem
*)bo
;
846 struct drm_i915_gem_pread pread
;
849 memset (&pread
, 0, sizeof (pread
));
850 pread
.handle
= bo_gem
->gem_handle
;
851 pread
.offset
= offset
;
853 pread
.data_ptr
= (uint64_t) (uintptr_t) data
;
855 ret
= ioctl (bufmgr_gem
->fd
, DRM_IOCTL_I915_GEM_PREAD
, &pread
);
856 } while (ret
== -1 && errno
== EINTR
);
858 fprintf (stderr
, "%s:%d: Error reading data from buffer %d: (%d %d) %s .\n",
860 bo_gem
->gem_handle
, (int) offset
, (int) size
,
866 /** Waits for all GPU rendering to the object to have completed. */
868 drm_intel_gem_bo_wait_rendering(drm_intel_bo
*bo
)
870 return drm_intel_gem_bo_start_gtt_access(bo
, 0);
874 * Sets the object to the GTT read and possibly write domain, used by the X
875 * 2D driver in the absence of kernel support to do drm_intel_gem_bo_map_gtt().
877 * In combination with drm_intel_gem_bo_pin() and manual fence management, we
878 * can do tiled pixmaps this way.
881 drm_intel_gem_bo_start_gtt_access(drm_intel_bo
*bo
, int write_enable
)
883 drm_intel_bufmgr_gem
*bufmgr_gem
= (drm_intel_bufmgr_gem
*)bo
->bufmgr
;
884 drm_intel_bo_gem
*bo_gem
= (drm_intel_bo_gem
*)bo
;
885 struct drm_i915_gem_set_domain set_domain
;
888 set_domain
.handle
= bo_gem
->gem_handle
;
889 set_domain
.read_domains
= I915_GEM_DOMAIN_GTT
;
890 set_domain
.write_domain
= write_enable
? I915_GEM_DOMAIN_GTT
: 0;
892 ret
= ioctl(bufmgr_gem
->fd
, DRM_IOCTL_I915_GEM_SET_DOMAIN
, &set_domain
);
893 } while (ret
== -1 && errno
== EINTR
);
895 fprintf (stderr
, "%s:%d: Error setting memory domains %d (%08x %08x): %s .\n",
897 bo_gem
->gem_handle
, set_domain
.read_domains
, set_domain
.write_domain
,
903 drm_intel_bufmgr_gem_destroy(drm_intel_bufmgr
*bufmgr
)
905 drm_intel_bufmgr_gem
*bufmgr_gem
= (drm_intel_bufmgr_gem
*)bufmgr
;
908 free(bufmgr_gem
->exec_objects
);
909 free(bufmgr_gem
->exec_bos
);
911 pthread_mutex_destroy(&bufmgr_gem
->lock
);
913 /* Free any cached buffer objects we were going to reuse */
914 for (i
= 0; i
< DRM_INTEL_GEM_BO_BUCKETS
; i
++) {
915 struct drm_intel_gem_bo_bucket
*bucket
= &bufmgr_gem
->cache_bucket
[i
];
916 drm_intel_bo_gem
*bo_gem
;
918 while (!DRMLISTEMPTY(&bucket
->head
)) {
919 bo_gem
= DRMLISTENTRY(drm_intel_bo_gem
, bucket
->head
.next
, head
);
920 DRMLISTDEL(&bo_gem
->head
);
921 bucket
->num_entries
--;
923 drm_intel_gem_bo_free(&bo_gem
->bo
);
931 * Adds the target buffer to the validation list and adds the relocation
932 * to the reloc_buffer's relocation list.
934 * The relocation entry at the given offset must already contain the
935 * precomputed relocation value, because the kernel will optimize out
936 * the relocation entry write when the buffer hasn't moved from the
937 * last known offset in target_bo.
940 drm_intel_gem_bo_emit_reloc(drm_intel_bo
*bo
, uint32_t offset
,
941 drm_intel_bo
*target_bo
, uint32_t target_offset
,
942 uint32_t read_domains
, uint32_t write_domain
)
944 drm_intel_bufmgr_gem
*bufmgr_gem
= (drm_intel_bufmgr_gem
*)bo
->bufmgr
;
945 drm_intel_bo_gem
*bo_gem
= (drm_intel_bo_gem
*)bo
;
946 drm_intel_bo_gem
*target_bo_gem
= (drm_intel_bo_gem
*)target_bo
;
948 pthread_mutex_lock(&bufmgr_gem
->lock
);
950 /* Create a new relocation list if needed */
951 if (bo_gem
->relocs
== NULL
)
952 drm_intel_setup_reloc_list(bo
);
955 assert(bo_gem
->reloc_count
< bufmgr_gem
->max_relocs
);
958 assert (offset
<= bo
->size
- 4);
959 assert ((write_domain
& (write_domain
-1)) == 0);
961 /* Make sure that we're not adding a reloc to something whose size has
962 * already been accounted for.
964 assert(!bo_gem
->used_as_reloc_target
);
965 bo_gem
->reloc_tree_size
+= target_bo_gem
->reloc_tree_size
;
966 bo_gem
->reloc_tree_fences
+= target_bo_gem
->reloc_tree_fences
;
968 /* Flag the target to disallow further relocations in it. */
969 target_bo_gem
->used_as_reloc_target
= 1;
971 bo_gem
->relocs
[bo_gem
->reloc_count
].offset
= offset
;
972 bo_gem
->relocs
[bo_gem
->reloc_count
].delta
= target_offset
;
973 bo_gem
->relocs
[bo_gem
->reloc_count
].target_handle
=
974 target_bo_gem
->gem_handle
;
975 bo_gem
->relocs
[bo_gem
->reloc_count
].read_domains
= read_domains
;
976 bo_gem
->relocs
[bo_gem
->reloc_count
].write_domain
= write_domain
;
977 bo_gem
->relocs
[bo_gem
->reloc_count
].presumed_offset
= target_bo
->offset
;
979 bo_gem
->reloc_target_bo
[bo_gem
->reloc_count
] = target_bo
;
980 drm_intel_gem_bo_reference_locked(target_bo
);
982 bo_gem
->reloc_count
++;
984 pthread_mutex_unlock(&bufmgr_gem
->lock
);
990 * Walk the tree of relocations rooted at BO and accumulate the list of
991 * validations to be performed and update the relocation buffers with
992 * index values into the validation list.
995 drm_intel_gem_bo_process_reloc(drm_intel_bo
*bo
)
997 drm_intel_bo_gem
*bo_gem
= (drm_intel_bo_gem
*)bo
;
1000 if (bo_gem
->relocs
== NULL
)
1003 for (i
= 0; i
< bo_gem
->reloc_count
; i
++) {
1004 drm_intel_bo
*target_bo
= bo_gem
->reloc_target_bo
[i
];
1006 /* Continue walking the tree depth-first. */
1007 drm_intel_gem_bo_process_reloc(target_bo
);
1009 /* Add the target to the validate list */
1010 drm_intel_add_validate_buffer(target_bo
);
1015 drm_intel_update_buffer_offsets (drm_intel_bufmgr_gem
*bufmgr_gem
)
1019 for (i
= 0; i
< bufmgr_gem
->exec_count
; i
++) {
1020 drm_intel_bo
*bo
= bufmgr_gem
->exec_bos
[i
];
1021 drm_intel_bo_gem
*bo_gem
= (drm_intel_bo_gem
*)bo
;
1023 /* Update the buffer offset */
1024 if (bufmgr_gem
->exec_objects
[i
].offset
!= bo
->offset
) {
1025 DBG("BO %d (%s) migrated: 0x%08lx -> 0x%08llx\n",
1026 bo_gem
->gem_handle
, bo_gem
->name
, bo
->offset
,
1027 (unsigned long long)bufmgr_gem
->exec_objects
[i
].offset
);
1028 bo
->offset
= bufmgr_gem
->exec_objects
[i
].offset
;
1034 drm_intel_gem_bo_exec(drm_intel_bo
*bo
, int used
,
1035 drm_clip_rect_t
*cliprects
, int num_cliprects
,
1038 drm_intel_bufmgr_gem
*bufmgr_gem
= (drm_intel_bufmgr_gem
*)bo
->bufmgr
;
1039 struct drm_i915_gem_execbuffer execbuf
;
1042 pthread_mutex_lock(&bufmgr_gem
->lock
);
1043 /* Update indices and set up the validate list. */
1044 drm_intel_gem_bo_process_reloc(bo
);
1046 /* Add the batch buffer to the validation list. There are no relocations
1049 drm_intel_add_validate_buffer(bo
);
1051 execbuf
.buffers_ptr
= (uintptr_t)bufmgr_gem
->exec_objects
;
1052 execbuf
.buffer_count
= bufmgr_gem
->exec_count
;
1053 execbuf
.batch_start_offset
= 0;
1054 execbuf
.batch_len
= used
;
1055 execbuf
.cliprects_ptr
= (uintptr_t)cliprects
;
1056 execbuf
.num_cliprects
= num_cliprects
;
1061 ret
= ioctl(bufmgr_gem
->fd
, DRM_IOCTL_I915_GEM_EXECBUFFER
, &execbuf
);
1062 } while (ret
!= 0 && errno
== EAGAIN
);
1064 if (ret
!= 0 && errno
== ENOMEM
) {
1065 fprintf(stderr
, "Execbuffer fails to pin. Estimate: %u. Actual: %u. Available: %u\n",
1066 drm_intel_gem_estimate_batch_space(bufmgr_gem
->exec_bos
,
1067 bufmgr_gem
->exec_count
),
1068 drm_intel_gem_compute_batch_space(bufmgr_gem
->exec_bos
,
1069 bufmgr_gem
->exec_count
),
1070 (unsigned int) bufmgr_gem
->gtt_size
);
1072 drm_intel_update_buffer_offsets (bufmgr_gem
);
1074 if (bufmgr_gem
->bufmgr
.debug
)
1075 drm_intel_gem_dump_validation_list(bufmgr_gem
);
1077 for (i
= 0; i
< bufmgr_gem
->exec_count
; i
++) {
1078 drm_intel_bo
*bo
= bufmgr_gem
->exec_bos
[i
];
1079 drm_intel_bo_gem
*bo_gem
= (drm_intel_bo_gem
*)bo
;
1081 /* Need to call swrast on next bo_map */
1084 /* Disconnect the buffer from the validate list */
1085 bo_gem
->validate_index
= -1;
1086 drm_intel_gem_bo_unreference_locked(bo
);
1087 bufmgr_gem
->exec_bos
[i
] = NULL
;
1089 bufmgr_gem
->exec_count
= 0;
1090 pthread_mutex_unlock(&bufmgr_gem
->lock
);
1096 drm_intel_gem_bo_pin(drm_intel_bo
*bo
, uint32_t alignment
)
1098 drm_intel_bufmgr_gem
*bufmgr_gem
= (drm_intel_bufmgr_gem
*)bo
->bufmgr
;
1099 drm_intel_bo_gem
*bo_gem
= (drm_intel_bo_gem
*)bo
;
1100 struct drm_i915_gem_pin pin
;
1103 memset(&pin
, 0, sizeof(pin
));
1104 pin
.handle
= bo_gem
->gem_handle
;
1105 pin
.alignment
= alignment
;
1108 ret
= ioctl(bufmgr_gem
->fd
, DRM_IOCTL_I915_GEM_PIN
, &pin
);
1109 } while (ret
== -1 && errno
== EINTR
);
1114 bo
->offset
= pin
.offset
;
1119 drm_intel_gem_bo_unpin(drm_intel_bo
*bo
)
1121 drm_intel_bufmgr_gem
*bufmgr_gem
= (drm_intel_bufmgr_gem
*)bo
->bufmgr
;
1122 drm_intel_bo_gem
*bo_gem
= (drm_intel_bo_gem
*)bo
;
1123 struct drm_i915_gem_unpin unpin
;
1126 memset(&unpin
, 0, sizeof(unpin
));
1127 unpin
.handle
= bo_gem
->gem_handle
;
1129 ret
= ioctl(bufmgr_gem
->fd
, DRM_IOCTL_I915_GEM_UNPIN
, &unpin
);
1137 drm_intel_gem_bo_set_tiling(drm_intel_bo
*bo
, uint32_t *tiling_mode
,
1140 drm_intel_bufmgr_gem
*bufmgr_gem
= (drm_intel_bufmgr_gem
*)bo
->bufmgr
;
1141 drm_intel_bo_gem
*bo_gem
= (drm_intel_bo_gem
*)bo
;
1142 struct drm_i915_gem_set_tiling set_tiling
;
1145 if (bo_gem
->global_name
== 0 && *tiling_mode
== bo_gem
->tiling_mode
)
1148 /* If we're going from non-tiling to tiling, bump fence count */
1149 if (bo_gem
->tiling_mode
== I915_TILING_NONE
)
1150 bo_gem
->reloc_tree_fences
++;
1152 memset(&set_tiling
, 0, sizeof(set_tiling
));
1153 set_tiling
.handle
= bo_gem
->gem_handle
;
1154 set_tiling
.tiling_mode
= *tiling_mode
;
1155 set_tiling
.stride
= stride
;
1157 ret
= ioctl(bufmgr_gem
->fd
, DRM_IOCTL_I915_GEM_SET_TILING
, &set_tiling
);
1159 *tiling_mode
= bo_gem
->tiling_mode
;
1162 bo_gem
->tiling_mode
= set_tiling
.tiling_mode
;
1163 bo_gem
->swizzle_mode
= set_tiling
.swizzle_mode
;
1165 /* If we're going from tiling to non-tiling, drop fence count */
1166 if (bo_gem
->tiling_mode
== I915_TILING_NONE
)
1167 bo_gem
->reloc_tree_fences
--;
1169 *tiling_mode
= bo_gem
->tiling_mode
;
1174 drm_intel_gem_bo_get_tiling(drm_intel_bo
*bo
, uint32_t *tiling_mode
,
1175 uint32_t *swizzle_mode
)
1177 drm_intel_bo_gem
*bo_gem
= (drm_intel_bo_gem
*)bo
;
1179 *tiling_mode
= bo_gem
->tiling_mode
;
1180 *swizzle_mode
= bo_gem
->swizzle_mode
;
1185 drm_intel_gem_bo_flink(drm_intel_bo
*bo
, uint32_t *name
)
1187 drm_intel_bufmgr_gem
*bufmgr_gem
= (drm_intel_bufmgr_gem
*)bo
->bufmgr
;
1188 drm_intel_bo_gem
*bo_gem
= (drm_intel_bo_gem
*)bo
;
1189 struct drm_gem_flink flink
;
1192 if (!bo_gem
->global_name
) {
1193 memset(&flink
, 0, sizeof(flink
));
1194 flink
.handle
= bo_gem
->gem_handle
;
1196 ret
= ioctl(bufmgr_gem
->fd
, DRM_IOCTL_GEM_FLINK
, &flink
);
1199 bo_gem
->global_name
= flink
.name
;
1200 bo_gem
->reusable
= 0;
1203 *name
= bo_gem
->global_name
;
1208 * Enables unlimited caching of buffer objects for reuse.
1210 * This is potentially very memory expensive, as the cache at each bucket
1211 * size is only bounded by how many buffers of that size we've managed to have
1212 * in flight at once.
1215 drm_intel_bufmgr_gem_enable_reuse(drm_intel_bufmgr
*bufmgr
)
1217 drm_intel_bufmgr_gem
*bufmgr_gem
= (drm_intel_bufmgr_gem
*)bufmgr
;
1220 for (i
= 0; i
< DRM_INTEL_GEM_BO_BUCKETS
; i
++) {
1221 bufmgr_gem
->cache_bucket
[i
].max_entries
= -1;
1226 * Return the additional aperture space required by the tree of buffer objects
1230 drm_intel_gem_bo_get_aperture_space(drm_intel_bo
*bo
)
1232 drm_intel_bo_gem
*bo_gem
= (drm_intel_bo_gem
*)bo
;
1236 if (bo
== NULL
|| bo_gem
->included_in_check_aperture
)
1240 bo_gem
->included_in_check_aperture
= 1;
1242 for (i
= 0; i
< bo_gem
->reloc_count
; i
++)
1243 total
+= drm_intel_gem_bo_get_aperture_space(bo_gem
->reloc_target_bo
[i
]);
1249 * Count the number of buffers in this list that need a fence reg
1251 * If the count is greater than the number of available regs, we'll have
1252 * to ask the caller to resubmit a batch with fewer tiled buffers.
1254 * This function over-counts if the same buffer is used multiple times.
1257 drm_intel_gem_total_fences(drm_intel_bo
**bo_array
, int count
)
1260 unsigned int total
= 0;
1262 for (i
= 0; i
< count
; i
++) {
1263 drm_intel_bo_gem
*bo_gem
= (drm_intel_bo_gem
*)bo_array
[i
];
1268 total
+= bo_gem
->reloc_tree_fences
;
1274 * Clear the flag set by drm_intel_gem_bo_get_aperture_space() so we're ready
1275 * for the next drm_intel_bufmgr_check_aperture_space() call.
1278 drm_intel_gem_bo_clear_aperture_space_flag(drm_intel_bo
*bo
)
1280 drm_intel_bo_gem
*bo_gem
= (drm_intel_bo_gem
*)bo
;
1283 if (bo
== NULL
|| !bo_gem
->included_in_check_aperture
)
1286 bo_gem
->included_in_check_aperture
= 0;
1288 for (i
= 0; i
< bo_gem
->reloc_count
; i
++)
1289 drm_intel_gem_bo_clear_aperture_space_flag(bo_gem
->reloc_target_bo
[i
]);
1293 * Return a conservative estimate for the amount of aperture required
1294 * for a collection of buffers. This may double-count some buffers.
1297 drm_intel_gem_estimate_batch_space(drm_intel_bo
**bo_array
, int count
)
1300 unsigned int total
= 0;
1302 for (i
= 0; i
< count
; i
++) {
1303 drm_intel_bo_gem
*bo_gem
= (drm_intel_bo_gem
*)bo_array
[i
];
1305 total
+= bo_gem
->reloc_tree_size
;
1311 * Return the amount of aperture needed for a collection of buffers.
1312 * This avoids double counting any buffers, at the cost of looking
1313 * at every buffer in the set.
1316 drm_intel_gem_compute_batch_space(drm_intel_bo
**bo_array
, int count
)
1319 unsigned int total
= 0;
1321 for (i
= 0; i
< count
; i
++) {
1322 total
+= drm_intel_gem_bo_get_aperture_space(bo_array
[i
]);
1323 /* For the first buffer object in the array, we get an accurate count
1324 * back for its reloc_tree size (since nothing had been flagged as
1325 * being counted yet). We can save that value out as a more
1326 * conservative reloc_tree_size that avoids double-counting target
1327 * buffers. Since the first buffer happens to usually be the batch
1328 * buffer in our callers, this can pull us back from doing the tree
1329 * walk on every new batch emit.
1332 drm_intel_bo_gem
*bo_gem
= (drm_intel_bo_gem
*)bo_array
[i
];
1333 bo_gem
->reloc_tree_size
= total
;
1337 for (i
= 0; i
< count
; i
++)
1338 drm_intel_gem_bo_clear_aperture_space_flag(bo_array
[i
]);
1343 * Return -1 if the batchbuffer should be flushed before attempting to
1344 * emit rendering referencing the buffers pointed to by bo_array.
1346 * This is required because if we try to emit a batchbuffer with relocations
1347 * to a tree of buffers that won't simultaneously fit in the aperture,
1348 * the rendering will return an error at a point where the software is not
1349 * prepared to recover from it.
1351 * However, we also want to emit the batchbuffer significantly before we reach
1352 * the limit, as a series of batchbuffers each of which references buffers
1353 * covering almost all of the aperture means that at each emit we end up
1354 * waiting to evict a buffer from the last rendering, and we get synchronous
1355 * performance. By emitting smaller batchbuffers, we eat some CPU overhead to
1356 * get better parallelism.
1359 drm_intel_gem_check_aperture_space(drm_intel_bo
**bo_array
, int count
)
1361 drm_intel_bufmgr_gem
*bufmgr_gem
= (drm_intel_bufmgr_gem
*)bo_array
[0]->bufmgr
;
1362 unsigned int total
= 0;
1363 unsigned int threshold
= bufmgr_gem
->gtt_size
* 3 / 4;
1366 /* Check for fence reg constraints if necessary */
1367 if (bufmgr_gem
->available_fences
) {
1368 total_fences
= drm_intel_gem_total_fences(bo_array
, count
);
1369 if (total_fences
> bufmgr_gem
->available_fences
)
1373 total
= drm_intel_gem_estimate_batch_space(bo_array
, count
);
1375 if (total
> threshold
)
1376 total
= drm_intel_gem_compute_batch_space(bo_array
, count
);
1378 if (total
> threshold
) {
1379 DBG("check_space: overflowed available aperture, %dkb vs %dkb\n",
1380 total
/ 1024, (int)bufmgr_gem
->gtt_size
/ 1024);
1383 DBG("drm_check_space: total %dkb vs bufgr %dkb\n", total
/ 1024 ,
1384 (int)bufmgr_gem
->gtt_size
/ 1024);
1390 * Disable buffer reuse for objects which are shared with the kernel
1391 * as scanout buffers
1394 drm_intel_gem_bo_disable_reuse(drm_intel_bo
*bo
)
1396 drm_intel_bo_gem
*bo_gem
= (drm_intel_bo_gem
*)bo
;
1398 bo_gem
->reusable
= 0;
1403 * Initializes the GEM buffer manager, which uses the kernel to allocate, map,
1404 * and manage map buffer objections.
1406 * \param fd File descriptor of the opened DRM device.
1409 drm_intel_bufmgr_gem_init(int fd
, int batch_size
)
1411 drm_intel_bufmgr_gem
*bufmgr_gem
;
1412 struct drm_i915_gem_get_aperture aperture
;
1413 drm_i915_getparam_t gp
;
1416 bufmgr_gem
= calloc(1, sizeof(*bufmgr_gem
));
1417 bufmgr_gem
->fd
= fd
;
1419 if (pthread_mutex_init(&bufmgr_gem
->lock
, NULL
) != 0) {
1424 ret
= ioctl(bufmgr_gem
->fd
, DRM_IOCTL_I915_GEM_GET_APERTURE
, &aperture
);
1427 bufmgr_gem
->gtt_size
= aperture
.aper_available_size
;
1429 fprintf(stderr
, "DRM_IOCTL_I915_GEM_APERTURE failed: %s\n",
1431 bufmgr_gem
->gtt_size
= 128 * 1024 * 1024;
1432 fprintf(stderr
, "Assuming %dkB available aperture size.\n"
1433 "May lead to reduced performance or incorrect rendering.\n",
1434 (int)bufmgr_gem
->gtt_size
/ 1024);
1437 gp
.param
= I915_PARAM_CHIPSET_ID
;
1438 gp
.value
= &bufmgr_gem
->pci_device
;
1439 ret
= ioctl(bufmgr_gem
->fd
, DRM_IOCTL_I915_GETPARAM
, &gp
);
1441 fprintf(stderr
, "get chip id failed: %d\n", ret
);
1442 fprintf(stderr
, "param: %d, val: %d\n", gp
.param
, *gp
.value
);
1445 if (!IS_I965G(bufmgr_gem
)) {
1446 gp
.param
= I915_PARAM_NUM_FENCES_AVAIL
;
1447 gp
.value
= &bufmgr_gem
->available_fences
;
1448 ret
= ioctl(bufmgr_gem
->fd
, DRM_IOCTL_I915_GETPARAM
, &gp
);
1450 fprintf(stderr
, "get fences failed: %d\n", ret
);
1451 fprintf(stderr
, "param: %d, val: %d\n", gp
.param
, *gp
.value
);
1452 bufmgr_gem
->available_fences
= 0;
1456 /* Let's go with one relocation per every 2 dwords (but round down a bit
1457 * since a power of two will mean an extra page allocation for the reloc
1460 * Every 4 was too few for the blender benchmark.
1462 bufmgr_gem
->max_relocs
= batch_size
/ sizeof(uint32_t) / 2 - 2;
1464 bufmgr_gem
->bufmgr
.bo_alloc
= drm_intel_gem_bo_alloc
;
1465 bufmgr_gem
->bufmgr
.bo_alloc_for_render
= drm_intel_gem_bo_alloc_for_render
;
1466 bufmgr_gem
->bufmgr
.bo_reference
= drm_intel_gem_bo_reference
;
1467 bufmgr_gem
->bufmgr
.bo_unreference
= drm_intel_gem_bo_unreference
;
1468 bufmgr_gem
->bufmgr
.bo_map
= drm_intel_gem_bo_map
;
1469 bufmgr_gem
->bufmgr
.bo_unmap
= drm_intel_gem_bo_unmap
;
1470 bufmgr_gem
->bufmgr
.bo_subdata
= drm_intel_gem_bo_subdata
;
1471 bufmgr_gem
->bufmgr
.bo_get_subdata
= drm_intel_gem_bo_get_subdata
;
1472 bufmgr_gem
->bufmgr
.bo_wait_rendering
= drm_intel_gem_bo_wait_rendering
;
1473 bufmgr_gem
->bufmgr
.bo_emit_reloc
= drm_intel_gem_bo_emit_reloc
;
1474 bufmgr_gem
->bufmgr
.bo_pin
= drm_intel_gem_bo_pin
;
1475 bufmgr_gem
->bufmgr
.bo_unpin
= drm_intel_gem_bo_unpin
;
1476 bufmgr_gem
->bufmgr
.bo_get_tiling
= drm_intel_gem_bo_get_tiling
;
1477 bufmgr_gem
->bufmgr
.bo_set_tiling
= drm_intel_gem_bo_set_tiling
;
1478 bufmgr_gem
->bufmgr
.bo_flink
= drm_intel_gem_bo_flink
;
1479 bufmgr_gem
->bufmgr
.bo_exec
= drm_intel_gem_bo_exec
;
1480 bufmgr_gem
->bufmgr
.destroy
= drm_intel_bufmgr_gem_destroy
;
1481 bufmgr_gem
->bufmgr
.debug
= 0;
1482 bufmgr_gem
->bufmgr
.check_aperture_space
= drm_intel_gem_check_aperture_space
;
1483 bufmgr_gem
->bufmgr
.bo_disable_reuse
= drm_intel_gem_bo_disable_reuse
;
1484 bufmgr_gem
->bufmgr
.get_pipe_from_crtc_id
= drm_intel_gem_get_pipe_from_crtc_id
;
1485 /* Initialize the linked lists for BO reuse cache. */
1486 for (i
= 0; i
< DRM_INTEL_GEM_BO_BUCKETS
; i
++)
1487 DRMINITLISTHEAD(&bufmgr_gem
->cache_bucket
[i
].head
);
1489 return &bufmgr_gem
->bufmgr
;