diff options
author | Eric Anholt <[email protected]> | 2016-10-12 10:30:41 -0700 |
---|---|---|
committer | Eric Anholt <[email protected]> | 2016-10-21 14:12:22 -0700 |
commit | 1c38ee380dc6c847c62e8a83978b31382f55bcff (patch) | |
tree | 2ed99fcbb1fdd9a923fa6c10bef506b6e4a927bd /src/gallium | |
parent | 9f755223821eaa0a889f8a10cccc66e15bf0abb4 (diff) |
vc4: Move simulator memory management to a u_mm.h heap.
Now we aren't limited to 256MB total allocated across a driver instance,
just 256MB at one time. We're still copying in and out, which should get
fixed.
Diffstat (limited to 'src/gallium')
-rw-r--r-- | src/gallium/drivers/vc4/vc4_bufmgr.c | 2 | ||||
-rw-r--r-- | src/gallium/drivers/vc4/vc4_context.h | 2 | ||||
-rw-r--r-- | src/gallium/drivers/vc4/vc4_screen.h | 4 | ||||
-rw-r--r-- | src/gallium/drivers/vc4/vc4_simulator.c | 238 | ||||
-rw-r--r-- | src/gallium/drivers/vc4/vc4_simulator_validate.h | 3 |
5 files changed, 208 insertions, 41 deletions
diff --git a/src/gallium/drivers/vc4/vc4_bufmgr.c b/src/gallium/drivers/vc4/vc4_bufmgr.c index 350761ee86c..1ed2e7fb9d3 100644 --- a/src/gallium/drivers/vc4/vc4_bufmgr.c +++ b/src/gallium/drivers/vc4/vc4_bufmgr.c @@ -349,6 +349,8 @@ vc4_bo_open_handle(struct vc4_screen *screen, bo->private = false; #ifdef USE_VC4_SIMULATOR + vc4_simulator_open_from_handle(screen->fd, winsys_stride, + bo->handle, bo->size); vc4_bo_map(bo); bo->simulator_winsys_map = bo->map; bo->simulator_winsys_stride = winsys_stride; diff --git a/src/gallium/drivers/vc4/vc4_context.h b/src/gallium/drivers/vc4/vc4_context.h index 249caebc1bc..5dd0ba645e5 100644 --- a/src/gallium/drivers/vc4/vc4_context.h +++ b/src/gallium/drivers/vc4/vc4_context.h @@ -429,6 +429,8 @@ int vc4_simulator_flush(struct vc4_context *vc4, struct drm_vc4_submit_cl *args, struct vc4_job *job); int vc4_simulator_ioctl(int fd, unsigned long request, void *arg); +void vc4_simulator_open_from_handle(int fd, uint32_t winsys_stride, + int handle, uint32_t size); static inline int vc4_ioctl(int fd, unsigned long request, void *arg) diff --git a/src/gallium/drivers/vc4/vc4_screen.h b/src/gallium/drivers/vc4/vc4_screen.h index 9fbe48c9a51..83bb5aacfcf 100644 --- a/src/gallium/drivers/vc4/vc4_screen.h +++ b/src/gallium/drivers/vc4/vc4_screen.h @@ -47,6 +47,8 @@ struct vc4_bo; #define VC4_MAX_MIP_LEVELS 12 #define VC4_MAX_TEXTURE_SAMPLERS 16 +struct vc4_simulator_file; + struct vc4_screen { struct pipe_screen base; int fd; @@ -83,6 +85,8 @@ struct vc4_screen { uint32_t bo_size; uint32_t bo_count; bool has_control_flow; + + struct vc4_simulator_file *sim_file; }; static inline struct vc4_screen * diff --git a/src/gallium/drivers/vc4/vc4_simulator.c b/src/gallium/drivers/vc4/vc4_simulator.c index e77819d9045..b9698d28dad 100644 --- a/src/gallium/drivers/vc4/vc4_simulator.c +++ b/src/gallium/drivers/vc4/vc4_simulator.c @@ -26,6 +26,7 @@ #include <sys/mman.h> #include "xf86drm.h" #include "util/u_memory.h" +#include "util/u_mm.h" #include "util/ralloc.h" #include "vc4_screen.h" @@ -40,55 +41,147 @@ static struct vc4_simulator_state { void *mem; ssize_t mem_size; + struct mem_block *heap; + struct mem_block *overflow; + + /** Mapping from GEM handle to struct vc4_simulator_bo * */ + struct hash_table *fd_map; int refcount; } sim_state = { .mutex = _MTX_INITIALIZER_NP, }; +/** Per-GEM-fd state for the simulator. */ +struct vc4_simulator_file { + int fd; + + /* This is weird -- we make a "vc4_device" per file, even though on + * the kernel side this is a global. We do this so that kernel code + * calling us for BO allocation can get to our screen. + */ + struct drm_device dev; + + /** Mapping from GEM handle to struct vc4_simulator_bo * */ + struct hash_table *bo_map; +}; + +/** Wrapper for drm_vc4_bo tracking the simulator-specific state. */ +struct vc4_simulator_bo { + struct drm_vc4_bo base; + struct vc4_simulator_file *file; + + /** Area for this BO within sim_state->mem */ + struct mem_block *block; + + int handle; +}; + +static void * +int_to_key(int key) +{ + return (void *)(uintptr_t)key; +} + +static struct vc4_simulator_file * +vc4_get_simulator_file_for_fd(int fd) +{ + struct hash_entry *entry = _mesa_hash_table_search(sim_state.fd_map, + int_to_key(fd + 1)); + return entry ? entry->data : NULL; +} + /* A marker placed just after each BO, then checked after rendering to make * sure it's still there. */ #define BO_SENTINEL 0xfedcba98 -#define OVERFLOW_SIZE (32 * 1024 * 1024) +#define PAGE_ALIGN2 12 -static struct drm_gem_cma_object * -vc4_wrap_bo_with_cma(struct drm_device *dev, struct vc4_bo *bo) +/** + * Allocates space in simulator memory and returns a tracking struct for it + * that also contains the drm_gem_cma_object struct. + */ +static struct vc4_simulator_bo * +vc4_create_simulator_bo(int fd, int handle, unsigned size) { - struct drm_vc4_bo *drm_bo = CALLOC_STRUCT(drm_vc4_bo); - struct drm_gem_cma_object *obj = &drm_bo->base; - uint32_t size = align(bo->size, 4096); + struct vc4_simulator_file *file = vc4_get_simulator_file_for_fd(fd); + struct vc4_simulator_bo *sim_bo = rzalloc(file, + struct vc4_simulator_bo); + struct drm_vc4_bo *bo = &sim_bo->base; + struct drm_gem_cma_object *obj = &bo->base; + size = align(size, 4096); + + sim_bo->file = file; + sim_bo->handle = handle; + + mtx_lock(&sim_state.mutex); + sim_bo->block = u_mmAllocMem(sim_state.heap, size + 4, PAGE_ALIGN2, 0); + mtx_unlock(&sim_state.mutex); + assert(sim_bo->block); - drm_bo->bo = bo; obj->base.size = size; - obj->base.dev = dev; - obj->vaddr = sim_state.mem + dev->simulator_mem_next; + obj->base.dev = &file->dev; + obj->vaddr = sim_state.mem + sim_bo->block->ofs; obj->paddr = simpenrose_hw_addr(obj->vaddr); - dev->simulator_mem_next += size + sizeof(uint32_t); - dev->simulator_mem_next = align(dev->simulator_mem_next, 4096); - assert(dev->simulator_mem_next <= sim_state.mem_size); + *(uint32_t *)(obj->vaddr + size) = BO_SENTINEL; + + /* A handle of 0 is used for vc4_gem.c internal allocations that + * don't need to go in the lookup table. + */ + if (handle != 0) { + mtx_lock(&sim_state.mutex); + _mesa_hash_table_insert(file->bo_map, int_to_key(handle), bo); + mtx_unlock(&sim_state.mutex); + } + + return sim_bo; +} - *(uint32_t *)(obj->vaddr + bo->size) = BO_SENTINEL; +static void +vc4_free_simulator_bo(struct vc4_simulator_bo *sim_bo) +{ + struct vc4_simulator_file *sim_file = sim_bo->file; + + mtx_lock(&sim_state.mutex); + u_mmFreeMem(sim_bo->block); + if (sim_bo->handle) { + struct hash_entry *entry = + _mesa_hash_table_search(sim_file->bo_map, + int_to_key(sim_bo->handle)); + _mesa_hash_table_remove(sim_file->bo_map, entry); + } + mtx_unlock(&sim_state.mutex); + ralloc_free(sim_bo); +} + +static struct vc4_simulator_bo * +vc4_get_simulator_bo(struct vc4_simulator_file *file, int gem_handle) +{ + mtx_lock(&sim_state.mutex); + struct hash_entry *entry = + _mesa_hash_table_search(file->bo_map, int_to_key(gem_handle)); + mtx_unlock(&sim_state.mutex); - return obj; + return entry ? entry->data : NULL; } struct drm_gem_cma_object * drm_gem_cma_create(struct drm_device *dev, size_t size) { - struct vc4_context *vc4 = dev->vc4; - struct vc4_screen *screen = vc4->screen; - - struct vc4_bo *bo = vc4_bo_alloc(screen, size, "simulator validate"); - return vc4_wrap_bo_with_cma(dev, bo); + struct vc4_screen *screen = dev->screen; + struct vc4_simulator_bo *sim_bo = vc4_create_simulator_bo(screen->fd, + 0, size); + return &sim_bo->base.base; } static int vc4_simulator_pin_bos(struct drm_device *dev, struct vc4_job *job, struct vc4_exec_info *exec) { + int fd = dev->screen->fd; + struct vc4_simulator_file *file = vc4_get_simulator_file_for_fd(fd); struct drm_vc4_submit_cl *args = exec->args; struct vc4_bo **bos = job->bo_pointers.base; @@ -96,9 +189,12 @@ vc4_simulator_pin_bos(struct drm_device *dev, struct vc4_job *job, exec->bo = calloc(exec->bo_count, sizeof(void *)); for (int i = 0; i < exec->bo_count; i++) { struct vc4_bo *bo = bos[i]; - struct drm_gem_cma_object *obj = vc4_wrap_bo_with_cma(dev, bo); + struct vc4_simulator_bo *sim_bo = + vc4_get_simulator_bo(file, bo->handle); + struct drm_vc4_bo *drm_bo = &sim_bo->base; + struct drm_gem_cma_object *obj = &drm_bo->base; - struct drm_vc4_bo *drm_bo = to_vc4_bo(&obj->base); + drm_bo->bo = bo; #if 0 fprintf(stderr, "bo hindex %d: %s\n", i, bo->name); #endif @@ -128,14 +224,14 @@ vc4_simulator_unpin_bos(struct vc4_exec_info *exec) struct drm_vc4_bo *drm_bo = to_vc4_bo(&obj->base); struct vc4_bo *bo = drm_bo->bo; - assert(*(uint32_t *)(obj->vaddr + bo->size) == BO_SENTINEL); + assert(*(uint32_t *)(obj->vaddr + + obj->base.size) == BO_SENTINEL); memcpy(bo->map, obj->vaddr, bo->size); if (drm_bo->validated_shader) { free(drm_bo->validated_shader->texture_samples); free(drm_bo->validated_shader); } - free(obj); } free(exec->bo); @@ -204,8 +300,8 @@ vc4_dump_to_file(struct vc4_exec_info *exec) /* Add the static overflow memory area. */ bo_state[i].handle = exec->bo_count; - bo_state[i].paddr = 0; - bo_state[i].size = OVERFLOW_SIZE; + bo_state[i].paddr = sim_state.overflow->ofs; + bo_state[i].size = sim_state.overflow->size; i++; fwrite(bo_state, sizeof(*bo_state), state->bo_count, f); @@ -221,8 +317,8 @@ vc4_dump_to_file(struct vc4_exec_info *exec) fwrite(cma_bo->vaddr, cma_bo->base.size, 1, f); } - void *overflow = calloc(1, OVERFLOW_SIZE); - fwrite(overflow, 1, OVERFLOW_SIZE, f); + void *overflow = calloc(1, sim_state.overflow->size); + fwrite(overflow, 1, sim_state.overflow->size, f); free(overflow); free(state); @@ -234,17 +330,16 @@ int vc4_simulator_flush(struct vc4_context *vc4, struct drm_vc4_submit_cl *args, struct vc4_job *job) { + struct vc4_screen *screen = vc4->screen; + int fd = screen->fd; + struct vc4_simulator_file *file = vc4_get_simulator_file_for_fd(fd); struct vc4_surface *csurf = vc4_surface(vc4->framebuffer.cbufs[0]); struct vc4_resource *ctex = csurf ? vc4_resource(csurf->base.texture) : NULL; uint32_t winsys_stride = ctex ? ctex->bo->simulator_winsys_stride : 0; uint32_t sim_stride = ctex ? ctex->slices[0].stride : 0; uint32_t row_len = MIN2(sim_stride, winsys_stride); struct vc4_exec_info exec; - struct drm_device local_dev = { - .vc4 = vc4, - .simulator_mem_next = OVERFLOW_SIZE, - }; - struct drm_device *dev = &local_dev; + struct drm_device *dev = &file->dev; int ret; memset(&exec, 0, sizeof(exec)); @@ -311,11 +406,12 @@ vc4_simulator_flush(struct vc4_context *vc4, list_for_each_entry_safe(struct drm_vc4_bo, bo, &exec.unref_list, unref_head) { + struct vc4_simulator_bo *sim_bo = (struct vc4_simulator_bo *)bo; + struct drm_gem_cma_object *obj = &sim_bo->base.base; list_del(&bo->unref_head); - assert(*(uint32_t *)(bo->base.vaddr + bo->bo->size) == + assert(*(uint32_t *)(obj->vaddr + obj->base.size) == BO_SENTINEL); - vc4_bo_unreference(&bo->bo); - free(bo); + vc4_free_simulator_bo(sim_bo); } if (ctex && ctex->bo->simulator_winsys_map) { @@ -330,6 +426,19 @@ vc4_simulator_flush(struct vc4_context *vc4, } /** + * Do fixups after a BO has been opened from a handle. + * + * This could be done at DRM_IOCTL_GEM_OPEN/DRM_IOCTL_GEM_PRIME_FD_TO_HANDLE + * time, but we're still using drmPrimeFDToHandle() so we have this helper to + * be called afterward instead. + */ +void vc4_simulator_open_from_handle(int fd, uint32_t winsys_stride, + int handle, uint32_t size) +{ + vc4_create_simulator_bo(fd, handle, size); +} + +/** * Simulated ioctl(fd, DRM_VC4_CREATE_BO) implementation. * * Making a VC4 BO is just a matter of making a corresponding BO on the host. @@ -349,6 +458,8 @@ vc4_simulator_create_bo_ioctl(int fd, struct drm_vc4_create_bo *args) args->handle = create.handle; + vc4_create_simulator_bo(fd, create.handle, args->size); + return ret; } @@ -376,6 +487,8 @@ vc4_simulator_create_shader_bo_ioctl(int fd, args->handle = create.handle; + vc4_create_simulator_bo(fd, create.handle, args->size); + struct drm_mode_map_dumb map = { .handle = create.handle }; @@ -411,6 +524,20 @@ vc4_simulator_mmap_bo_ioctl(int fd, struct drm_vc4_mmap_bo *args) } static int +vc4_simulator_gem_close_ioctl(int fd, struct drm_gem_close *args) +{ + /* Free the simulator's internal tracking. */ + struct vc4_simulator_file *file = vc4_get_simulator_file_for_fd(fd); + struct vc4_simulator_bo *sim_bo = vc4_get_simulator_bo(file, + args->handle); + + vc4_free_simulator_bo(sim_bo); + + /* Pass the call on down. */ + return drmIoctl(fd, DRM_IOCTL_GEM_CLOSE, args); +} + +static int vc4_simulator_get_param_ioctl(int fd, struct drm_vc4_get_param *args) { switch (args->param) { @@ -456,8 +583,10 @@ vc4_simulator_ioctl(int fd, unsigned long request, void *args) case DRM_IOCTL_VC4_GET_PARAM: return vc4_simulator_get_param_ioctl(fd, args); - case DRM_IOCTL_GEM_OPEN: case DRM_IOCTL_GEM_CLOSE: + return vc4_simulator_gem_close_ioctl(fd, args); + + case DRM_IOCTL_GEM_OPEN: case DRM_IOCTL_GEM_FLINK: return drmIoctl(fd, request, args); default: @@ -466,8 +595,8 @@ vc4_simulator_ioctl(int fd, unsigned long request, void *args) } } -void -vc4_simulator_init(struct vc4_screen *screen) +static void +vc4_simulator_init_global(void) { mtx_lock(&sim_state.mutex); if (sim_state.refcount++) { @@ -479,6 +608,7 @@ vc4_simulator_init(struct vc4_screen *screen) sim_state.mem = calloc(sim_state.mem_size, 1); if (!sim_state.mem) abort(); + sim_state.heap = u_mmInit(0, sim_state.mem_size); /* We supply our own memory so that we can have more aperture * available (256MB instead of simpenrose's default 64MB). @@ -492,9 +622,37 @@ vc4_simulator_init(struct vc4_screen *screen) * up over the whole lifetime of simpenrose (not reused on each * flush), so it had better be big. */ - simpenrose_supply_overflow_mem(0, OVERFLOW_SIZE); + sim_state.overflow = u_mmAllocMem(sim_state.heap, 32 * 1024 * 1024, + PAGE_ALIGN2, 0); + simpenrose_supply_overflow_mem(sim_state.overflow->ofs, + sim_state.overflow->size); mtx_unlock(&sim_state.mutex); + + sim_state.fd_map = + _mesa_hash_table_create(NULL, + _mesa_hash_pointer, + _mesa_key_pointer_equal); +} + +void +vc4_simulator_init(struct vc4_screen *screen) +{ + vc4_simulator_init_global(); + + screen->sim_file = rzalloc(screen, struct vc4_simulator_file); + + screen->sim_file->bo_map = + _mesa_hash_table_create(screen->sim_file, + _mesa_hash_pointer, + _mesa_key_pointer_equal); + + mtx_lock(&sim_state.mutex); + _mesa_hash_table_insert(sim_state.fd_map, int_to_key(screen->fd + 1), + screen->sim_file); + mtx_unlock(&sim_state.mutex); + + screen->sim_file->dev.screen = screen; } void @@ -502,6 +660,8 @@ vc4_simulator_destroy(struct vc4_screen *screen) { mtx_lock(&sim_state.mutex); if (!--sim_state.refcount) { + _mesa_hash_table_destroy(sim_state.fd_map, NULL); + u_mmDestroy(sim_state.heap); free(sim_state.mem); /* No memsetting it, because it contains the mutex. */ } diff --git a/src/gallium/drivers/vc4/vc4_simulator_validate.h b/src/gallium/drivers/vc4/vc4_simulator_validate.h index 1352c9bafd2..d507b5fb632 100644 --- a/src/gallium/drivers/vc4/vc4_simulator_validate.h +++ b/src/gallium/drivers/vc4/vc4_simulator_validate.h @@ -78,8 +78,7 @@ typedef uint16_t u16; typedef uint32_t u32; struct drm_device { - struct vc4_context *vc4; - uint32_t simulator_mem_next; + struct vc4_screen *screen; }; struct drm_gem_object { |