i965/bufmgr: Add comments about GTT coherency issues.
[mesa.git] / src / mesa / drivers / dri / i965 / brw_bufmgr.c
1 /**************************************************************************
2 *
3 * Copyright © 2007 Red Hat Inc.
4 * Copyright © 2007-2012 Intel Corporation
5 * Copyright 2006 Tungsten Graphics, Inc., Bismarck, ND., USA
6 * All Rights Reserved.
7 *
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:
15 *
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.
23 *
24 * The above copyright notice and this permission notice (including the
25 * next paragraph) shall be included in all copies or substantial portions
26 * of the Software.
27 *
28 *
29 **************************************************************************/
30 /*
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>
35 */
36
37 #ifdef HAVE_CONFIG_H
38 #include "config.h"
39 #endif
40
41 #include <xf86drm.h>
42 #include <util/u_atomic.h>
43 #include <fcntl.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <unistd.h>
48 #include <assert.h>
49 #include <pthread.h>
50 #include <sys/ioctl.h>
51 #include <sys/stat.h>
52 #include <sys/types.h>
53 #include <stdbool.h>
54
55 #include "errno.h"
56 #ifndef ETIME
57 #define ETIME ETIMEDOUT
58 #endif
59 #include "common/gen_clflush.h"
60 #include "common/gen_debug.h"
61 #include "common/gen_device_info.h"
62 #include "libdrm_macros.h"
63 #include "main/macros.h"
64 #include "util/macros.h"
65 #include "util/hash_table.h"
66 #include "util/list.h"
67 #include "brw_bufmgr.h"
68 #include "brw_context.h"
69 #include "string.h"
70
71 #include "i915_drm.h"
72
73 #ifdef HAVE_VALGRIND
74 #include <valgrind.h>
75 #include <memcheck.h>
76 #define VG(x) x
77 #else
78 #define VG(x)
79 #endif
80
81 /* VALGRIND_FREELIKE_BLOCK unfortunately does not actually undo the earlier
82 * VALGRIND_MALLOCLIKE_BLOCK but instead leaves vg convinced the memory is
83 * leaked. All because it does not call VG(cli_free) from its
84 * VG_USERREQ__FREELIKE_BLOCK handler. Instead of treating the memory like
85 * and allocation, we mark it available for use upon mmapping and remove
86 * it upon unmapping.
87 */
88 #define VG_DEFINED(ptr, size) VG(VALGRIND_MAKE_MEM_DEFINED(ptr, size))
89 #define VG_NOACCESS(ptr, size) VG(VALGRIND_MAKE_MEM_NOACCESS(ptr, size))
90
91 #define memclear(s) memset(&s, 0, sizeof(s))
92
93 #define FILE_DEBUG_FLAG DEBUG_BUFMGR
94
95 static inline int
96 atomic_add_unless(int *v, int add, int unless)
97 {
98 int c, old;
99 c = p_atomic_read(v);
100 while (c != unless && (old = p_atomic_cmpxchg(v, c, c + add)) != c)
101 c = old;
102 return c == unless;
103 }
104
105 struct bo_cache_bucket {
106 struct list_head head;
107 uint64_t size;
108 };
109
110 struct brw_bufmgr {
111 int fd;
112
113 pthread_mutex_t lock;
114
115 /** Array of lists of cached gem objects of power-of-two sizes */
116 struct bo_cache_bucket cache_bucket[14 * 4];
117 int num_buckets;
118 time_t time;
119
120 struct hash_table *name_table;
121 struct hash_table *handle_table;
122
123 bool has_llc:1;
124 bool has_mmap_wc:1;
125 bool bo_reuse:1;
126 };
127
128 static int bo_set_tiling_internal(struct brw_bo *bo, uint32_t tiling_mode,
129 uint32_t stride);
130
131 static void bo_free(struct brw_bo *bo);
132
133 static uint32_t
134 key_hash_uint(const void *key)
135 {
136 return _mesa_hash_data(key, 4);
137 }
138
139 static bool
140 key_uint_equal(const void *a, const void *b)
141 {
142 return *((unsigned *) a) == *((unsigned *) b);
143 }
144
145 static struct brw_bo *
146 hash_find_bo(struct hash_table *ht, unsigned int key)
147 {
148 struct hash_entry *entry = _mesa_hash_table_search(ht, &key);
149 return entry ? (struct brw_bo *) entry->data : NULL;
150 }
151
152 static uint64_t
153 bo_tile_size(struct brw_bufmgr *bufmgr, uint64_t size, uint32_t tiling)
154 {
155 if (tiling == I915_TILING_NONE)
156 return size;
157
158 /* 965+ just need multiples of page size for tiling */
159 return ALIGN(size, 4096);
160 }
161
162 /*
163 * Round a given pitch up to the minimum required for X tiling on a
164 * given chip. We use 512 as the minimum to allow for a later tiling
165 * change.
166 */
167 static uint32_t
168 bo_tile_pitch(struct brw_bufmgr *bufmgr, uint32_t pitch, uint32_t tiling)
169 {
170 unsigned long tile_width;
171
172 /* If untiled, then just align it so that we can do rendering
173 * to it with the 3D engine.
174 */
175 if (tiling == I915_TILING_NONE)
176 return ALIGN(pitch, 64);
177
178 if (tiling == I915_TILING_X)
179 tile_width = 512;
180 else
181 tile_width = 128;
182
183 /* 965 is flexible */
184 return ALIGN(pitch, tile_width);
185 }
186
187 static struct bo_cache_bucket *
188 bucket_for_size(struct brw_bufmgr *bufmgr, uint64_t size)
189 {
190 int i;
191
192 for (i = 0; i < bufmgr->num_buckets; i++) {
193 struct bo_cache_bucket *bucket = &bufmgr->cache_bucket[i];
194 if (bucket->size >= size) {
195 return bucket;
196 }
197 }
198
199 return NULL;
200 }
201
202 inline void
203 brw_bo_reference(struct brw_bo *bo)
204 {
205 p_atomic_inc(&bo->refcount);
206 }
207
208 int
209 brw_bo_busy(struct brw_bo *bo)
210 {
211 struct brw_bufmgr *bufmgr = bo->bufmgr;
212 struct drm_i915_gem_busy busy;
213 int ret;
214
215 memclear(busy);
216 busy.handle = bo->gem_handle;
217
218 ret = drmIoctl(bufmgr->fd, DRM_IOCTL_I915_GEM_BUSY, &busy);
219 if (ret == 0) {
220 bo->idle = !busy.busy;
221 return busy.busy;
222 }
223 return false;
224 }
225
226 int
227 brw_bo_madvise(struct brw_bo *bo, int state)
228 {
229 struct drm_i915_gem_madvise madv;
230
231 memclear(madv);
232 madv.handle = bo->gem_handle;
233 madv.madv = state;
234 madv.retained = 1;
235 drmIoctl(bo->bufmgr->fd, DRM_IOCTL_I915_GEM_MADVISE, &madv);
236
237 return madv.retained;
238 }
239
240 /* drop the oldest entries that have been purged by the kernel */
241 static void
242 brw_bo_cache_purge_bucket(struct brw_bufmgr *bufmgr,
243 struct bo_cache_bucket *bucket)
244 {
245 list_for_each_entry_safe(struct brw_bo, bo, &bucket->head, head) {
246 if (brw_bo_madvise(bo, I915_MADV_DONTNEED))
247 break;
248
249 list_del(&bo->head);
250 bo_free(bo);
251 }
252 }
253
254 static struct brw_bo *
255 bo_alloc_internal(struct brw_bufmgr *bufmgr,
256 const char *name,
257 uint64_t size,
258 unsigned flags,
259 uint32_t tiling_mode,
260 uint32_t stride, uint64_t alignment)
261 {
262 struct brw_bo *bo;
263 unsigned int page_size = getpagesize();
264 int ret;
265 struct bo_cache_bucket *bucket;
266 bool alloc_from_cache;
267 uint64_t bo_size;
268 bool for_render = false;
269 bool zeroed = false;
270
271 if (flags & BO_ALLOC_FOR_RENDER)
272 for_render = true;
273
274 if (flags & BO_ALLOC_ZEROED)
275 zeroed = true;
276
277 /* FOR_RENDER really means "I'm ok with a busy BO". This doesn't really
278 * jive with ZEROED as we have to wait for it to be idle before we can
279 * memset. Just disallow that combination.
280 */
281 assert(!(for_render && zeroed));
282
283 /* Round the allocated size up to a power of two number of pages. */
284 bucket = bucket_for_size(bufmgr, size);
285
286 /* If we don't have caching at this size, don't actually round the
287 * allocation up.
288 */
289 if (bucket == NULL) {
290 bo_size = size;
291 if (bo_size < page_size)
292 bo_size = page_size;
293 } else {
294 bo_size = bucket->size;
295 }
296
297 pthread_mutex_lock(&bufmgr->lock);
298 /* Get a buffer out of the cache if available */
299 retry:
300 alloc_from_cache = false;
301 if (bucket != NULL && !list_empty(&bucket->head)) {
302 if (for_render && !zeroed) {
303 /* Allocate new render-target BOs from the tail (MRU)
304 * of the list, as it will likely be hot in the GPU
305 * cache and in the aperture for us. If the caller
306 * asked us to zero the buffer, we don't want this
307 * because we are going to mmap it.
308 */
309 bo = LIST_ENTRY(struct brw_bo, bucket->head.prev, head);
310 list_del(&bo->head);
311 alloc_from_cache = true;
312 bo->align = alignment;
313 } else {
314 assert(alignment == 0);
315 /* For non-render-target BOs (where we're probably
316 * going to map it first thing in order to fill it
317 * with data), check if the last BO in the cache is
318 * unbusy, and only reuse in that case. Otherwise,
319 * allocating a new buffer is probably faster than
320 * waiting for the GPU to finish.
321 */
322 bo = LIST_ENTRY(struct brw_bo, bucket->head.next, head);
323 if (!brw_bo_busy(bo)) {
324 alloc_from_cache = true;
325 list_del(&bo->head);
326 }
327 }
328
329 if (alloc_from_cache) {
330 if (!brw_bo_madvise(bo, I915_MADV_WILLNEED)) {
331 bo_free(bo);
332 brw_bo_cache_purge_bucket(bufmgr, bucket);
333 goto retry;
334 }
335
336 if (bo_set_tiling_internal(bo, tiling_mode, stride)) {
337 bo_free(bo);
338 goto retry;
339 }
340
341 if (zeroed) {
342 void *map = brw_bo_map(NULL, bo, MAP_WRITE | MAP_RAW);
343 if (!map) {
344 bo_free(bo);
345 goto retry;
346 }
347 memset(map, 0, bo_size);
348 }
349 }
350 }
351
352 if (!alloc_from_cache) {
353 struct drm_i915_gem_create create;
354
355 bo = calloc(1, sizeof(*bo));
356 if (!bo)
357 goto err;
358
359 bo->size = bo_size;
360 bo->idle = true;
361
362 memclear(create);
363 create.size = bo_size;
364
365 /* All new BOs we get from the kernel are zeroed, so we don't need to
366 * worry about that here.
367 */
368 ret = drmIoctl(bufmgr->fd, DRM_IOCTL_I915_GEM_CREATE, &create);
369 if (ret != 0) {
370 free(bo);
371 goto err;
372 }
373
374 bo->gem_handle = create.handle;
375 _mesa_hash_table_insert(bufmgr->handle_table, &bo->gem_handle, bo);
376
377 bo->bufmgr = bufmgr;
378 bo->align = alignment;
379
380 bo->tiling_mode = I915_TILING_NONE;
381 bo->swizzle_mode = I915_BIT_6_SWIZZLE_NONE;
382 bo->stride = 0;
383
384 if (bo_set_tiling_internal(bo, tiling_mode, stride))
385 goto err_free;
386
387 /* Calling set_domain() will allocate pages for the BO outside of the
388 * struct mutex lock in the kernel, which is more efficient than waiting
389 * to create them during the first execbuf that uses the BO.
390 */
391 struct drm_i915_gem_set_domain sd = {
392 .handle = bo->gem_handle,
393 .read_domains = I915_GEM_DOMAIN_CPU,
394 .write_domain = 0,
395 };
396
397 if (drmIoctl(bo->bufmgr->fd, DRM_IOCTL_I915_GEM_SET_DOMAIN, &sd) != 0)
398 goto err_free;
399 }
400
401 bo->name = name;
402 p_atomic_set(&bo->refcount, 1);
403 bo->reusable = true;
404 bo->cache_coherent = bufmgr->has_llc;
405
406 pthread_mutex_unlock(&bufmgr->lock);
407
408 DBG("bo_create: buf %d (%s) %ldb\n", bo->gem_handle, bo->name, size);
409
410 return bo;
411
412 err_free:
413 bo_free(bo);
414 err:
415 pthread_mutex_unlock(&bufmgr->lock);
416 return NULL;
417 }
418
419 struct brw_bo *
420 brw_bo_alloc(struct brw_bufmgr *bufmgr,
421 const char *name, uint64_t size, uint64_t alignment)
422 {
423 return bo_alloc_internal(bufmgr, name, size, 0, I915_TILING_NONE, 0, 0);
424 }
425
426 struct brw_bo *
427 brw_bo_alloc_tiled(struct brw_bufmgr *bufmgr, const char *name,
428 uint64_t size, uint32_t tiling_mode, uint32_t pitch,
429 unsigned flags)
430 {
431 return bo_alloc_internal(bufmgr, name, size, flags, tiling_mode, pitch, 0);
432 }
433
434 struct brw_bo *
435 brw_bo_alloc_tiled_2d(struct brw_bufmgr *bufmgr, const char *name,
436 int x, int y, int cpp, uint32_t tiling,
437 uint32_t *pitch, unsigned flags)
438 {
439 uint64_t size;
440 uint32_t stride;
441 unsigned long aligned_y, height_alignment;
442
443 /* If we're tiled, our allocations are in 8 or 32-row blocks,
444 * so failure to align our height means that we won't allocate
445 * enough pages.
446 *
447 * If we're untiled, we still have to align to 2 rows high
448 * because the data port accesses 2x2 blocks even if the
449 * bottom row isn't to be rendered, so failure to align means
450 * we could walk off the end of the GTT and fault. This is
451 * documented on 965, and may be the case on older chipsets
452 * too so we try to be careful.
453 */
454 aligned_y = y;
455 height_alignment = 2;
456
457 if (tiling == I915_TILING_X)
458 height_alignment = 8;
459 else if (tiling == I915_TILING_Y)
460 height_alignment = 32;
461 aligned_y = ALIGN(y, height_alignment);
462
463 stride = x * cpp;
464 stride = bo_tile_pitch(bufmgr, stride, tiling);
465 size = stride * aligned_y;
466 size = bo_tile_size(bufmgr, size, tiling);
467 *pitch = stride;
468
469 if (tiling == I915_TILING_NONE)
470 stride = 0;
471
472 return bo_alloc_internal(bufmgr, name, size, flags, tiling, stride, 0);
473 }
474
475 /**
476 * Returns a brw_bo wrapping the given buffer object handle.
477 *
478 * This can be used when one application needs to pass a buffer object
479 * to another.
480 */
481 struct brw_bo *
482 brw_bo_gem_create_from_name(struct brw_bufmgr *bufmgr,
483 const char *name, unsigned int handle)
484 {
485 struct brw_bo *bo;
486 int ret;
487 struct drm_gem_open open_arg;
488 struct drm_i915_gem_get_tiling get_tiling;
489
490 /* At the moment most applications only have a few named bo.
491 * For instance, in a DRI client only the render buffers passed
492 * between X and the client are named. And since X returns the
493 * alternating names for the front/back buffer a linear search
494 * provides a sufficiently fast match.
495 */
496 pthread_mutex_lock(&bufmgr->lock);
497 bo = hash_find_bo(bufmgr->name_table, handle);
498 if (bo) {
499 brw_bo_reference(bo);
500 goto out;
501 }
502
503 memclear(open_arg);
504 open_arg.name = handle;
505 ret = drmIoctl(bufmgr->fd, DRM_IOCTL_GEM_OPEN, &open_arg);
506 if (ret != 0) {
507 DBG("Couldn't reference %s handle 0x%08x: %s\n",
508 name, handle, strerror(errno));
509 bo = NULL;
510 goto out;
511 }
512 /* Now see if someone has used a prime handle to get this
513 * object from the kernel before by looking through the list
514 * again for a matching gem_handle
515 */
516 bo = hash_find_bo(bufmgr->handle_table, open_arg.handle);
517 if (bo) {
518 brw_bo_reference(bo);
519 goto out;
520 }
521
522 bo = calloc(1, sizeof(*bo));
523 if (!bo)
524 goto out;
525
526 p_atomic_set(&bo->refcount, 1);
527
528 bo->size = open_arg.size;
529 bo->offset64 = 0;
530 bo->bufmgr = bufmgr;
531 bo->gem_handle = open_arg.handle;
532 bo->name = name;
533 bo->global_name = handle;
534 bo->reusable = false;
535 bo->external = true;
536
537 _mesa_hash_table_insert(bufmgr->handle_table, &bo->gem_handle, bo);
538 _mesa_hash_table_insert(bufmgr->name_table, &bo->global_name, bo);
539
540 memclear(get_tiling);
541 get_tiling.handle = bo->gem_handle;
542 ret = drmIoctl(bufmgr->fd, DRM_IOCTL_I915_GEM_GET_TILING, &get_tiling);
543 if (ret != 0)
544 goto err_unref;
545
546 bo->tiling_mode = get_tiling.tiling_mode;
547 bo->swizzle_mode = get_tiling.swizzle_mode;
548 /* XXX stride is unknown */
549 DBG("bo_create_from_handle: %d (%s)\n", handle, bo->name);
550
551 out:
552 pthread_mutex_unlock(&bufmgr->lock);
553 return bo;
554
555 err_unref:
556 bo_free(bo);
557 pthread_mutex_unlock(&bufmgr->lock);
558 return NULL;
559 }
560
561 static void
562 bo_free(struct brw_bo *bo)
563 {
564 struct brw_bufmgr *bufmgr = bo->bufmgr;
565 struct drm_gem_close close;
566 struct hash_entry *entry;
567 int ret;
568
569 if (bo->map_cpu) {
570 VG_NOACCESS(bo->map_cpu, bo->size);
571 drm_munmap(bo->map_cpu, bo->size);
572 }
573 if (bo->map_wc) {
574 VG_NOACCESS(bo->map_wc, bo->size);
575 drm_munmap(bo->map_wc, bo->size);
576 }
577 if (bo->map_gtt) {
578 VG_NOACCESS(bo->map_gtt, bo->size);
579 drm_munmap(bo->map_gtt, bo->size);
580 }
581
582 if (bo->global_name) {
583 entry = _mesa_hash_table_search(bufmgr->name_table, &bo->global_name);
584 _mesa_hash_table_remove(bufmgr->name_table, entry);
585 }
586 entry = _mesa_hash_table_search(bufmgr->handle_table, &bo->gem_handle);
587 _mesa_hash_table_remove(bufmgr->handle_table, entry);
588
589 /* Close this object */
590 memclear(close);
591 close.handle = bo->gem_handle;
592 ret = drmIoctl(bufmgr->fd, DRM_IOCTL_GEM_CLOSE, &close);
593 if (ret != 0) {
594 DBG("DRM_IOCTL_GEM_CLOSE %d failed (%s): %s\n",
595 bo->gem_handle, bo->name, strerror(errno));
596 }
597 free(bo);
598 }
599
600 /** Frees all cached buffers significantly older than @time. */
601 static void
602 cleanup_bo_cache(struct brw_bufmgr *bufmgr, time_t time)
603 {
604 int i;
605
606 if (bufmgr->time == time)
607 return;
608
609 for (i = 0; i < bufmgr->num_buckets; i++) {
610 struct bo_cache_bucket *bucket = &bufmgr->cache_bucket[i];
611
612 list_for_each_entry_safe(struct brw_bo, bo, &bucket->head, head) {
613 if (time - bo->free_time <= 1)
614 break;
615
616 list_del(&bo->head);
617
618 bo_free(bo);
619 }
620 }
621
622 bufmgr->time = time;
623 }
624
625 static void
626 bo_unreference_final(struct brw_bo *bo, time_t time)
627 {
628 struct brw_bufmgr *bufmgr = bo->bufmgr;
629 struct bo_cache_bucket *bucket;
630
631 DBG("bo_unreference final: %d (%s)\n", bo->gem_handle, bo->name);
632
633 bucket = bucket_for_size(bufmgr, bo->size);
634 /* Put the buffer into our internal cache for reuse if we can. */
635 if (bufmgr->bo_reuse && bo->reusable && bucket != NULL &&
636 brw_bo_madvise(bo, I915_MADV_DONTNEED)) {
637 bo->free_time = time;
638
639 bo->name = NULL;
640 bo->kflags = 0;
641
642 list_addtail(&bo->head, &bucket->head);
643 } else {
644 bo_free(bo);
645 }
646 }
647
648 void
649 brw_bo_unreference(struct brw_bo *bo)
650 {
651 if (bo == NULL)
652 return;
653
654 assert(p_atomic_read(&bo->refcount) > 0);
655
656 if (atomic_add_unless(&bo->refcount, -1, 1)) {
657 struct brw_bufmgr *bufmgr = bo->bufmgr;
658 struct timespec time;
659
660 clock_gettime(CLOCK_MONOTONIC, &time);
661
662 pthread_mutex_lock(&bufmgr->lock);
663
664 if (p_atomic_dec_zero(&bo->refcount)) {
665 bo_unreference_final(bo, time.tv_sec);
666 cleanup_bo_cache(bufmgr, time.tv_sec);
667 }
668
669 pthread_mutex_unlock(&bufmgr->lock);
670 }
671 }
672
673 static void
674 bo_wait_with_stall_warning(struct brw_context *brw,
675 struct brw_bo *bo,
676 const char *action)
677 {
678 double elapsed = unlikely(brw && brw->perf_debug) ? -get_time() : 0.0;
679
680 brw_bo_wait_rendering(bo);
681
682 if (unlikely(brw && brw->perf_debug)) {
683 elapsed += get_time();
684 if (elapsed > 1e-5) /* 0.01ms */
685 perf_debug("%s a busy \"%s\" BO stalled and took %.03f ms.\n",
686 action, bo->name, elapsed * 1000);
687 }
688 }
689
690 static void
691 print_flags(unsigned flags)
692 {
693 if (flags & MAP_READ)
694 DBG("READ ");
695 if (flags & MAP_WRITE)
696 DBG("WRITE ");
697 if (flags & MAP_ASYNC)
698 DBG("ASYNC ");
699 if (flags & MAP_PERSISTENT)
700 DBG("PERSISTENT ");
701 if (flags & MAP_COHERENT)
702 DBG("COHERENT ");
703 if (flags & MAP_RAW)
704 DBG("RAW ");
705 DBG("\n");
706 }
707
708 static void *
709 brw_bo_map_cpu(struct brw_context *brw, struct brw_bo *bo, unsigned flags)
710 {
711 struct brw_bufmgr *bufmgr = bo->bufmgr;
712
713 /* We disallow CPU maps for writing to non-coherent buffers, as the
714 * CPU map can become invalidated when a batch is flushed out, which
715 * can happen at unpredictable times. You should use WC maps instead.
716 */
717 assert(bo->cache_coherent || !(flags & MAP_WRITE));
718
719 if (!bo->map_cpu) {
720 struct drm_i915_gem_mmap mmap_arg;
721 void *map;
722
723 DBG("brw_bo_map_cpu: %d (%s)\n", bo->gem_handle, bo->name);
724
725 memclear(mmap_arg);
726 mmap_arg.handle = bo->gem_handle;
727 mmap_arg.size = bo->size;
728 int ret = drmIoctl(bufmgr->fd, DRM_IOCTL_I915_GEM_MMAP, &mmap_arg);
729 if (ret != 0) {
730 ret = -errno;
731 DBG("%s:%d: Error mapping buffer %d (%s): %s .\n",
732 __FILE__, __LINE__, bo->gem_handle, bo->name, strerror(errno));
733 return NULL;
734 }
735 map = (void *) (uintptr_t) mmap_arg.addr_ptr;
736 VG_DEFINED(map, bo->size);
737
738 if (p_atomic_cmpxchg(&bo->map_cpu, NULL, map)) {
739 VG_NOACCESS(map, bo->size);
740 drm_munmap(map, bo->size);
741 }
742 }
743 assert(bo->map_cpu);
744
745 DBG("brw_bo_map_cpu: %d (%s) -> %p, ", bo->gem_handle, bo->name,
746 bo->map_cpu);
747 print_flags(flags);
748
749 if (!(flags & MAP_ASYNC)) {
750 bo_wait_with_stall_warning(brw, bo, "CPU mapping");
751 }
752
753 if (!bo->cache_coherent) {
754 /* If we're reusing an existing CPU mapping, the CPU caches may
755 * contain stale data from the last time we read from that mapping.
756 * (With the BO cache, it might even be data from a previous buffer!)
757 * Even if it's a brand new mapping, the kernel may have zeroed the
758 * buffer via CPU writes.
759 *
760 * We need to invalidate those cachelines so that we see the latest
761 * contents, and so long as we only read from the CPU mmap we do not
762 * need to write those cachelines back afterwards.
763 */
764 gen_invalidate_range(bo->map_cpu, bo->size);
765 }
766
767 return bo->map_cpu;
768 }
769
770 static void *
771 brw_bo_map_wc(struct brw_context *brw, struct brw_bo *bo, unsigned flags)
772 {
773 struct brw_bufmgr *bufmgr = bo->bufmgr;
774
775 if (!bufmgr->has_mmap_wc)
776 return NULL;
777
778 if (!bo->map_wc) {
779 struct drm_i915_gem_mmap mmap_arg;
780 void *map;
781
782 DBG("brw_bo_map_wc: %d (%s)\n", bo->gem_handle, bo->name);
783
784 memclear(mmap_arg);
785 mmap_arg.handle = bo->gem_handle;
786 mmap_arg.size = bo->size;
787 mmap_arg.flags = I915_MMAP_WC;
788 int ret = drmIoctl(bufmgr->fd, DRM_IOCTL_I915_GEM_MMAP, &mmap_arg);
789 if (ret != 0) {
790 ret = -errno;
791 DBG("%s:%d: Error mapping buffer %d (%s): %s .\n",
792 __FILE__, __LINE__, bo->gem_handle, bo->name, strerror(errno));
793 return NULL;
794 }
795
796 map = (void *) (uintptr_t) mmap_arg.addr_ptr;
797 VG_DEFINED(map, bo->size);
798
799 if (p_atomic_cmpxchg(&bo->map_wc, NULL, map)) {
800 VG_NOACCESS(map, bo->size);
801 drm_munmap(map, bo->size);
802 }
803 }
804 assert(bo->map_wc);
805
806 DBG("brw_bo_map_wc: %d (%s) -> %p\n", bo->gem_handle, bo->name, bo->map_wc);
807 print_flags(flags);
808
809 if (!(flags & MAP_ASYNC)) {
810 bo_wait_with_stall_warning(brw, bo, "WC mapping");
811 }
812
813 return bo->map_wc;
814 }
815
816 /**
817 * Perform an uncached mapping via the GTT.
818 *
819 * Write access through the GTT is not quite fully coherent. On low power
820 * systems especially, like modern Atoms, we can observe reads from RAM before
821 * the write via GTT has landed. A write memory barrier that flushes the Write
822 * Combining Buffer (i.e. sfence/mfence) is not sufficient to order the later
823 * read after the write as the GTT write suffers a small delay through the GTT
824 * indirection. The kernel uses an uncached mmio read to ensure the GTT write
825 * is ordered with reads (either by the GPU, WB or WC) and unconditionally
826 * flushes prior to execbuf submission. However, if we are not informing the
827 * kernel about our GTT writes, it will not flush before earlier access, such
828 * as when using the cmdparser. Similarly, we need to be careful if we should
829 * ever issue a CPU read immediately following a GTT write.
830 *
831 * Telling the kernel about write access also has one more important
832 * side-effect. Upon receiving notification about the write, it cancels any
833 * scanout buffering for FBC/PSR and friends. Later FBC/PSR is then flushed by
834 * either SW_FINISH or DIRTYFB. The presumption is that we never write to the
835 * actual scanout via a mmaping, only to a backbuffer and so all the FBC/PSR
836 * tracking is handled on the buffer exchange instead.
837 */
838 static void *
839 brw_bo_map_gtt(struct brw_context *brw, struct brw_bo *bo, unsigned flags)
840 {
841 struct brw_bufmgr *bufmgr = bo->bufmgr;
842
843 /* Get a mapping of the buffer if we haven't before. */
844 if (bo->map_gtt == NULL) {
845 struct drm_i915_gem_mmap_gtt mmap_arg;
846 void *map;
847
848 DBG("bo_map_gtt: mmap %d (%s)\n", bo->gem_handle, bo->name);
849
850 memclear(mmap_arg);
851 mmap_arg.handle = bo->gem_handle;
852
853 /* Get the fake offset back... */
854 int ret = drmIoctl(bufmgr->fd, DRM_IOCTL_I915_GEM_MMAP_GTT, &mmap_arg);
855 if (ret != 0) {
856 DBG("%s:%d: Error preparing buffer map %d (%s): %s .\n",
857 __FILE__, __LINE__, bo->gem_handle, bo->name, strerror(errno));
858 return NULL;
859 }
860
861 /* and mmap it. */
862 map = drm_mmap(0, bo->size, PROT_READ | PROT_WRITE,
863 MAP_SHARED, bufmgr->fd, mmap_arg.offset);
864 if (map == MAP_FAILED) {
865 DBG("%s:%d: Error mapping buffer %d (%s): %s .\n",
866 __FILE__, __LINE__, bo->gem_handle, bo->name, strerror(errno));
867 return NULL;
868 }
869
870 /* We don't need to use VALGRIND_MALLOCLIKE_BLOCK because Valgrind will
871 * already intercept this mmap call. However, for consistency between
872 * all the mmap paths, we mark the pointer as defined now and mark it
873 * as inaccessible afterwards.
874 */
875 VG_DEFINED(map, bo->size);
876
877 if (p_atomic_cmpxchg(&bo->map_gtt, NULL, map)) {
878 VG_NOACCESS(map, bo->size);
879 drm_munmap(map, bo->size);
880 }
881 }
882 assert(bo->map_gtt);
883
884 DBG("bo_map_gtt: %d (%s) -> %p, ", bo->gem_handle, bo->name, bo->map_gtt);
885 print_flags(flags);
886
887 if (!(flags & MAP_ASYNC)) {
888 bo_wait_with_stall_warning(brw, bo, "GTT mapping");
889 }
890
891 return bo->map_gtt;
892 }
893
894 static bool
895 can_map_cpu(struct brw_bo *bo, unsigned flags)
896 {
897 if (bo->cache_coherent)
898 return true;
899
900 /* If PERSISTENT or COHERENT are set, the mmapping needs to remain valid
901 * across batch flushes where the kernel will change cache domains of the
902 * bo, invalidating continued access to the CPU mmap on non-LLC device.
903 *
904 * Similarly, ASYNC typically means that the buffer will be accessed via
905 * both the CPU and the GPU simultaneously. Batches may be executed that
906 * use the BO even while it is mapped. While OpenGL technically disallows
907 * most drawing while non-persistent mappings are active, we may still use
908 * the GPU for blits or other operations, causing batches to happen at
909 * inconvenient times.
910 */
911 if (flags & (MAP_PERSISTENT | MAP_COHERENT | MAP_ASYNC))
912 return false;
913
914 return !(flags & MAP_WRITE);
915 }
916
917 void *
918 brw_bo_map(struct brw_context *brw, struct brw_bo *bo, unsigned flags)
919 {
920 if (bo->tiling_mode != I915_TILING_NONE && !(flags & MAP_RAW))
921 return brw_bo_map_gtt(brw, bo, flags);
922
923 void *map;
924
925 if (can_map_cpu(bo, flags))
926 map = brw_bo_map_cpu(brw, bo, flags);
927 else
928 map = brw_bo_map_wc(brw, bo, flags);
929
930 /* Allow the attempt to fail by falling back to the GTT where necessary.
931 *
932 * Not every buffer can be mmaped directly using the CPU (or WC), for
933 * example buffers that wrap stolen memory or are imported from other
934 * devices. For those, we have little choice but to use a GTT mmapping.
935 * However, if we use a slow GTT mmapping for reads where we expected fast
936 * access, that order of magnitude difference in throughput will be clearly
937 * expressed by angry users.
938 *
939 * We skip MAP_RAW because we want to avoid map_gtt's fence detiling.
940 */
941 if (!map && !(flags & MAP_RAW)) {
942 perf_debug("Fallback GTT mapping for %s with access flags %x\n",
943 bo->name, flags);
944 map = brw_bo_map_gtt(brw, bo, flags);
945 }
946
947 return map;
948 }
949
950 int
951 brw_bo_subdata(struct brw_bo *bo, uint64_t offset,
952 uint64_t size, const void *data)
953 {
954 struct brw_bufmgr *bufmgr = bo->bufmgr;
955 struct drm_i915_gem_pwrite pwrite;
956 int ret;
957
958 memclear(pwrite);
959 pwrite.handle = bo->gem_handle;
960 pwrite.offset = offset;
961 pwrite.size = size;
962 pwrite.data_ptr = (uint64_t) (uintptr_t) data;
963 ret = drmIoctl(bufmgr->fd, DRM_IOCTL_I915_GEM_PWRITE, &pwrite);
964 if (ret != 0) {
965 ret = -errno;
966 DBG("%s:%d: Error writing data to buffer %d: "
967 "(%"PRIu64" %"PRIu64") %s .\n",
968 __FILE__, __LINE__, bo->gem_handle, offset, size, strerror(errno));
969 }
970
971 return ret;
972 }
973
974 /** Waits for all GPU rendering with the object to have completed. */
975 void
976 brw_bo_wait_rendering(struct brw_bo *bo)
977 {
978 /* We require a kernel recent enough for WAIT_IOCTL support.
979 * See intel_init_bufmgr()
980 */
981 brw_bo_wait(bo, -1);
982 }
983
984 /**
985 * Waits on a BO for the given amount of time.
986 *
987 * @bo: buffer object to wait for
988 * @timeout_ns: amount of time to wait in nanoseconds.
989 * If value is less than 0, an infinite wait will occur.
990 *
991 * Returns 0 if the wait was successful ie. the last batch referencing the
992 * object has completed within the allotted time. Otherwise some negative return
993 * value describes the error. Of particular interest is -ETIME when the wait has
994 * failed to yield the desired result.
995 *
996 * Similar to brw_bo_wait_rendering except a timeout parameter allows
997 * the operation to give up after a certain amount of time. Another subtle
998 * difference is the internal locking semantics are different (this variant does
999 * not hold the lock for the duration of the wait). This makes the wait subject
1000 * to a larger userspace race window.
1001 *
1002 * The implementation shall wait until the object is no longer actively
1003 * referenced within a batch buffer at the time of the call. The wait will
1004 * not guarantee that the buffer is re-issued via another thread, or an flinked
1005 * handle. Userspace must make sure this race does not occur if such precision
1006 * is important.
1007 *
1008 * Note that some kernels have broken the inifite wait for negative values
1009 * promise, upgrade to latest stable kernels if this is the case.
1010 */
1011 int
1012 brw_bo_wait(struct brw_bo *bo, int64_t timeout_ns)
1013 {
1014 struct brw_bufmgr *bufmgr = bo->bufmgr;
1015 struct drm_i915_gem_wait wait;
1016 int ret;
1017
1018 /* If we know it's idle, don't bother with the kernel round trip */
1019 if (bo->idle && !bo->external)
1020 return 0;
1021
1022 memclear(wait);
1023 wait.bo_handle = bo->gem_handle;
1024 wait.timeout_ns = timeout_ns;
1025 ret = drmIoctl(bufmgr->fd, DRM_IOCTL_I915_GEM_WAIT, &wait);
1026 if (ret == -1)
1027 return -errno;
1028
1029 return ret;
1030 }
1031
1032 void
1033 brw_bufmgr_destroy(struct brw_bufmgr *bufmgr)
1034 {
1035 pthread_mutex_destroy(&bufmgr->lock);
1036
1037 /* Free any cached buffer objects we were going to reuse */
1038 for (int i = 0; i < bufmgr->num_buckets; i++) {
1039 struct bo_cache_bucket *bucket = &bufmgr->cache_bucket[i];
1040
1041 list_for_each_entry_safe(struct brw_bo, bo, &bucket->head, head) {
1042 list_del(&bo->head);
1043
1044 bo_free(bo);
1045 }
1046 }
1047
1048 _mesa_hash_table_destroy(bufmgr->name_table, NULL);
1049 _mesa_hash_table_destroy(bufmgr->handle_table, NULL);
1050
1051 free(bufmgr);
1052 }
1053
1054 static int
1055 bo_set_tiling_internal(struct brw_bo *bo, uint32_t tiling_mode,
1056 uint32_t stride)
1057 {
1058 struct brw_bufmgr *bufmgr = bo->bufmgr;
1059 struct drm_i915_gem_set_tiling set_tiling;
1060 int ret;
1061
1062 if (bo->global_name == 0 &&
1063 tiling_mode == bo->tiling_mode && stride == bo->stride)
1064 return 0;
1065
1066 memset(&set_tiling, 0, sizeof(set_tiling));
1067 do {
1068 /* set_tiling is slightly broken and overwrites the
1069 * input on the error path, so we have to open code
1070 * rmIoctl.
1071 */
1072 set_tiling.handle = bo->gem_handle;
1073 set_tiling.tiling_mode = tiling_mode;
1074 set_tiling.stride = stride;
1075
1076 ret = ioctl(bufmgr->fd, DRM_IOCTL_I915_GEM_SET_TILING, &set_tiling);
1077 } while (ret == -1 && (errno == EINTR || errno == EAGAIN));
1078 if (ret == -1)
1079 return -errno;
1080
1081 bo->tiling_mode = set_tiling.tiling_mode;
1082 bo->swizzle_mode = set_tiling.swizzle_mode;
1083 bo->stride = set_tiling.stride;
1084 return 0;
1085 }
1086
1087 int
1088 brw_bo_get_tiling(struct brw_bo *bo, uint32_t *tiling_mode,
1089 uint32_t *swizzle_mode)
1090 {
1091 *tiling_mode = bo->tiling_mode;
1092 *swizzle_mode = bo->swizzle_mode;
1093 return 0;
1094 }
1095
1096 struct brw_bo *
1097 brw_bo_gem_create_from_prime(struct brw_bufmgr *bufmgr, int prime_fd)
1098 {
1099 int ret;
1100 uint32_t handle;
1101 struct brw_bo *bo;
1102 struct drm_i915_gem_get_tiling get_tiling;
1103
1104 pthread_mutex_lock(&bufmgr->lock);
1105 ret = drmPrimeFDToHandle(bufmgr->fd, prime_fd, &handle);
1106 if (ret) {
1107 DBG("create_from_prime: failed to obtain handle from fd: %s\n",
1108 strerror(errno));
1109 pthread_mutex_unlock(&bufmgr->lock);
1110 return NULL;
1111 }
1112
1113 /*
1114 * See if the kernel has already returned this buffer to us. Just as
1115 * for named buffers, we must not create two bo's pointing at the same
1116 * kernel object
1117 */
1118 bo = hash_find_bo(bufmgr->handle_table, handle);
1119 if (bo) {
1120 brw_bo_reference(bo);
1121 goto out;
1122 }
1123
1124 bo = calloc(1, sizeof(*bo));
1125 if (!bo)
1126 goto out;
1127
1128 p_atomic_set(&bo->refcount, 1);
1129
1130 /* Determine size of bo. The fd-to-handle ioctl really should
1131 * return the size, but it doesn't. If we have kernel 3.12 or
1132 * later, we can lseek on the prime fd to get the size. Older
1133 * kernels will just fail, in which case we fall back to the
1134 * provided (estimated or guess size). */
1135 ret = lseek(prime_fd, 0, SEEK_END);
1136 if (ret != -1)
1137 bo->size = ret;
1138
1139 bo->bufmgr = bufmgr;
1140
1141 bo->gem_handle = handle;
1142 _mesa_hash_table_insert(bufmgr->handle_table, &bo->gem_handle, bo);
1143
1144 bo->name = "prime";
1145 bo->reusable = false;
1146 bo->external = true;
1147
1148 memclear(get_tiling);
1149 get_tiling.handle = bo->gem_handle;
1150 if (drmIoctl(bufmgr->fd, DRM_IOCTL_I915_GEM_GET_TILING, &get_tiling))
1151 goto err;
1152
1153 bo->tiling_mode = get_tiling.tiling_mode;
1154 bo->swizzle_mode = get_tiling.swizzle_mode;
1155 /* XXX stride is unknown */
1156
1157 out:
1158 pthread_mutex_unlock(&bufmgr->lock);
1159 return bo;
1160
1161 err:
1162 bo_free(bo);
1163 pthread_mutex_unlock(&bufmgr->lock);
1164 return NULL;
1165 }
1166
1167 int
1168 brw_bo_gem_export_to_prime(struct brw_bo *bo, int *prime_fd)
1169 {
1170 struct brw_bufmgr *bufmgr = bo->bufmgr;
1171
1172 if (drmPrimeHandleToFD(bufmgr->fd, bo->gem_handle,
1173 DRM_CLOEXEC, prime_fd) != 0)
1174 return -errno;
1175
1176 bo->reusable = false;
1177 bo->external = true;
1178
1179 return 0;
1180 }
1181
1182 int
1183 brw_bo_flink(struct brw_bo *bo, uint32_t *name)
1184 {
1185 struct brw_bufmgr *bufmgr = bo->bufmgr;
1186
1187 if (!bo->global_name) {
1188 struct drm_gem_flink flink;
1189
1190 memclear(flink);
1191 flink.handle = bo->gem_handle;
1192 if (drmIoctl(bufmgr->fd, DRM_IOCTL_GEM_FLINK, &flink))
1193 return -errno;
1194
1195 pthread_mutex_lock(&bufmgr->lock);
1196 if (!bo->global_name) {
1197 bo->global_name = flink.name;
1198 bo->reusable = false;
1199 bo->external = true;
1200
1201 _mesa_hash_table_insert(bufmgr->name_table, &bo->global_name, bo);
1202 }
1203 pthread_mutex_unlock(&bufmgr->lock);
1204 }
1205
1206 *name = bo->global_name;
1207 return 0;
1208 }
1209
1210 /**
1211 * Enables unlimited caching of buffer objects for reuse.
1212 *
1213 * This is potentially very memory expensive, as the cache at each bucket
1214 * size is only bounded by how many buffers of that size we've managed to have
1215 * in flight at once.
1216 */
1217 void
1218 brw_bufmgr_enable_reuse(struct brw_bufmgr *bufmgr)
1219 {
1220 bufmgr->bo_reuse = true;
1221 }
1222
1223 static void
1224 add_bucket(struct brw_bufmgr *bufmgr, int size)
1225 {
1226 unsigned int i = bufmgr->num_buckets;
1227
1228 assert(i < ARRAY_SIZE(bufmgr->cache_bucket));
1229
1230 list_inithead(&bufmgr->cache_bucket[i].head);
1231 bufmgr->cache_bucket[i].size = size;
1232 bufmgr->num_buckets++;
1233 }
1234
1235 static void
1236 init_cache_buckets(struct brw_bufmgr *bufmgr)
1237 {
1238 uint64_t size, cache_max_size = 64 * 1024 * 1024;
1239
1240 /* OK, so power of two buckets was too wasteful of memory.
1241 * Give 3 other sizes between each power of two, to hopefully
1242 * cover things accurately enough. (The alternative is
1243 * probably to just go for exact matching of sizes, and assume
1244 * that for things like composited window resize the tiled
1245 * width/height alignment and rounding of sizes to pages will
1246 * get us useful cache hit rates anyway)
1247 */
1248 add_bucket(bufmgr, 4096);
1249 add_bucket(bufmgr, 4096 * 2);
1250 add_bucket(bufmgr, 4096 * 3);
1251
1252 /* Initialize the linked lists for BO reuse cache. */
1253 for (size = 4 * 4096; size <= cache_max_size; size *= 2) {
1254 add_bucket(bufmgr, size);
1255
1256 add_bucket(bufmgr, size + size * 1 / 4);
1257 add_bucket(bufmgr, size + size * 2 / 4);
1258 add_bucket(bufmgr, size + size * 3 / 4);
1259 }
1260 }
1261
1262 uint32_t
1263 brw_create_hw_context(struct brw_bufmgr *bufmgr)
1264 {
1265 struct drm_i915_gem_context_create create;
1266 int ret;
1267
1268 memclear(create);
1269 ret = drmIoctl(bufmgr->fd, DRM_IOCTL_I915_GEM_CONTEXT_CREATE, &create);
1270 if (ret != 0) {
1271 DBG("DRM_IOCTL_I915_GEM_CONTEXT_CREATE failed: %s\n", strerror(errno));
1272 return 0;
1273 }
1274
1275 return create.ctx_id;
1276 }
1277
1278 void
1279 brw_destroy_hw_context(struct brw_bufmgr *bufmgr, uint32_t ctx_id)
1280 {
1281 struct drm_i915_gem_context_destroy d = {.ctx_id = ctx_id };
1282
1283 if (ctx_id != 0 &&
1284 drmIoctl(bufmgr->fd, DRM_IOCTL_I915_GEM_CONTEXT_DESTROY, &d) != 0) {
1285 fprintf(stderr, "DRM_IOCTL_I915_GEM_CONTEXT_DESTROY failed: %s\n",
1286 strerror(errno));
1287 }
1288 }
1289
1290 int
1291 brw_reg_read(struct brw_bufmgr *bufmgr, uint32_t offset, uint64_t *result)
1292 {
1293 struct drm_i915_reg_read reg_read;
1294 int ret;
1295
1296 memclear(reg_read);
1297 reg_read.offset = offset;
1298
1299 ret = drmIoctl(bufmgr->fd, DRM_IOCTL_I915_REG_READ, &reg_read);
1300
1301 *result = reg_read.val;
1302 return ret;
1303 }
1304
1305 static int
1306 gem_param(int fd, int name)
1307 {
1308 drm_i915_getparam_t gp;
1309 int v = -1; /* No param uses (yet) the sign bit, reserve it for errors */
1310
1311 memset(&gp, 0, sizeof(gp));
1312 gp.param = name;
1313 gp.value = &v;
1314 if (drmIoctl(fd, DRM_IOCTL_I915_GETPARAM, &gp))
1315 return -1;
1316
1317 return v;
1318 }
1319
1320 /**
1321 * Initializes the GEM buffer manager, which uses the kernel to allocate, map,
1322 * and manage map buffer objections.
1323 *
1324 * \param fd File descriptor of the opened DRM device.
1325 */
1326 struct brw_bufmgr *
1327 brw_bufmgr_init(struct gen_device_info *devinfo, int fd, int batch_size)
1328 {
1329 struct brw_bufmgr *bufmgr;
1330
1331 bufmgr = calloc(1, sizeof(*bufmgr));
1332 if (bufmgr == NULL)
1333 return NULL;
1334
1335 /* Handles to buffer objects belong to the device fd and are not
1336 * reference counted by the kernel. If the same fd is used by
1337 * multiple parties (threads sharing the same screen bufmgr, or
1338 * even worse the same device fd passed to multiple libraries)
1339 * ownership of those handles is shared by those independent parties.
1340 *
1341 * Don't do this! Ensure that each library/bufmgr has its own device
1342 * fd so that its namespace does not clash with another.
1343 */
1344 bufmgr->fd = fd;
1345
1346 if (pthread_mutex_init(&bufmgr->lock, NULL) != 0) {
1347 free(bufmgr);
1348 return NULL;
1349 }
1350
1351 bufmgr->has_llc = devinfo->has_llc;
1352 bufmgr->has_mmap_wc = gem_param(fd, I915_PARAM_MMAP_VERSION) > 0;
1353
1354 init_cache_buckets(bufmgr);
1355
1356 bufmgr->name_table =
1357 _mesa_hash_table_create(NULL, key_hash_uint, key_uint_equal);
1358 bufmgr->handle_table =
1359 _mesa_hash_table_create(NULL, key_hash_uint, key_uint_equal);
1360
1361 return bufmgr;
1362 }