From a42d7157849832485e63850abaf341b10c952ea0 Mon Sep 17 00:00:00 2001 From: Rafael Antognolli Date: Fri, 14 Dec 2018 13:20:45 -0800 Subject: [PATCH] i965/bufmgr: Factor out GEM_MMAP ioctl from mmap_cpu and mmap_wc. We want to add a new ioctl for mmap'ing buffers, so let's avoid duplicating that code on both functions by extracting it from them first. v2: Update helper function names (Ken). Reviewed-by: Kenneth Graunke Reviewed-by: Jordan Justen Part-of: --- src/mesa/drivers/dri/i965/brw_bufmgr.c | 55 ++++++++++++++------------ 1 file changed, 29 insertions(+), 26 deletions(-) diff --git a/src/mesa/drivers/dri/i965/brw_bufmgr.c b/src/mesa/drivers/dri/i965/brw_bufmgr.c index e3c5ec3e51a..d7c65f00690 100644 --- a/src/mesa/drivers/dri/i965/brw_bufmgr.c +++ b/src/mesa/drivers/dri/i965/brw_bufmgr.c @@ -958,10 +958,36 @@ print_flags(unsigned flags) } static void * -brw_bo_map_cpu(struct brw_context *brw, struct brw_bo *bo, unsigned flags) +brw_bo_gem_mmap_legacy(struct brw_context *brw, struct brw_bo *bo, bool wc) { struct brw_bufmgr *bufmgr = bo->bufmgr; + struct drm_i915_gem_mmap mmap_arg = { + .handle = bo->gem_handle, + .size = bo->size, + .flags = wc ? I915_MMAP_WC : 0, + }; + + int ret = drmIoctl(bufmgr->fd, DRM_IOCTL_I915_GEM_MMAP, &mmap_arg); + if (ret != 0) { + DBG("%s:%d: Error mapping buffer %d (%s): %s .\n", + __FILE__, __LINE__, bo->gem_handle, bo->name, strerror(errno)); + return NULL; + } + void *map = (void *) (uintptr_t) mmap_arg.addr_ptr; + + return map; +} + +static void * +brw_bo_gem_mmap(struct brw_context *brw, struct brw_bo *bo, bool wc) +{ + return brw_bo_gem_mmap_legacy(brw, bo, wc); +} + +static void * +brw_bo_map_cpu(struct brw_context *brw, struct brw_bo *bo, unsigned flags) +{ /* We disallow CPU maps for writing to non-coherent buffers, as the * CPU map can become invalidated when a batch is flushed out, which * can happen at unpredictable times. You should use WC maps instead. @@ -971,17 +997,7 @@ brw_bo_map_cpu(struct brw_context *brw, struct brw_bo *bo, unsigned flags) if (!bo->map_cpu) { DBG("brw_bo_map_cpu: %d (%s)\n", bo->gem_handle, bo->name); - struct drm_i915_gem_mmap mmap_arg = { - .handle = bo->gem_handle, - .size = bo->size, - }; - int ret = drmIoctl(bufmgr->fd, DRM_IOCTL_I915_GEM_MMAP, &mmap_arg); - if (ret != 0) { - DBG("%s:%d: Error mapping buffer %d (%s): %s .\n", - __FILE__, __LINE__, bo->gem_handle, bo->name, strerror(errno)); - return NULL; - } - void *map = (void *) (uintptr_t) mmap_arg.addr_ptr; + void *map = brw_bo_gem_mmap(brw, bo, false); VG_DEFINED(map, bo->size); if (p_atomic_cmpxchg(&bo->map_cpu, NULL, map)) { @@ -1032,20 +1048,7 @@ brw_bo_map_wc(struct brw_context *brw, struct brw_bo *bo, unsigned flags) if (!bo->map_wc) { DBG("brw_bo_map_wc: %d (%s)\n", bo->gem_handle, bo->name); - - struct drm_i915_gem_mmap mmap_arg = { - .handle = bo->gem_handle, - .size = bo->size, - .flags = I915_MMAP_WC, - }; - int ret = drmIoctl(bufmgr->fd, DRM_IOCTL_I915_GEM_MMAP, &mmap_arg); - if (ret != 0) { - DBG("%s:%d: Error mapping buffer %d (%s): %s .\n", - __FILE__, __LINE__, bo->gem_handle, bo->name, strerror(errno)); - return NULL; - } - - void *map = (void *) (uintptr_t) mmap_arg.addr_ptr; + void *map = brw_bo_gem_mmap(brw, bo, true); VG_DEFINED(map, bo->size); if (p_atomic_cmpxchg(&bo->map_wc, NULL, map)) { -- 2.30.2