Path: blob/21.2-virgl/src/gallium/drivers/vc4/vc4_simulator.c
4570 views
/*1* Copyright © 2014 Broadcom2*3* Permission is hereby granted, free of charge, to any person obtaining a4* copy of this software and associated documentation files (the "Software"),5* to deal in the Software without restriction, including without limitation6* the rights to use, copy, modify, merge, publish, distribute, sublicense,7* and/or sell copies of the Software, and to permit persons to whom the8* Software is furnished to do so, subject to the following conditions:9*10* The above copyright notice and this permission notice (including the next11* paragraph) shall be included in all copies or substantial portions of the12* Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR15* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,16* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL17* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER18* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING19* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS20* IN THE SOFTWARE.21*/2223/**24* @file vc4_simulator.c25*26* Implements VC4 simulation on top of a non-VC4 GEM fd.27*28* This file's goal is to emulate the VC4 ioctls' behavior in the kernel on29* top of the simpenrose software simulator. Generally, VC4 driver BOs have a30* GEM-side copy of their contents and a simulator-side memory area that the31* GEM contents get copied into during simulation. Once simulation is done,32* the simulator's data is copied back out to the GEM BOs, so that rendering33* appears on the screen as if actual hardware rendering had been done.34*35* One of the limitations of this code is that we shouldn't really need a36* GEM-side BO for non-window-system BOs. However, do we need unique BO37* handles for each of our GEM bos so that this file can look up its state38* from the handle passed in at submit ioctl time (also, a couple of places39* outside of this file still call ioctls directly on the fd).40*41* Another limitation is that BO import doesn't work unless the underlying42* window system's BO size matches what VC4 is going to use, which of course43* doesn't work out in practice. This means that for now, only DRI3 (VC444* makes the winsys BOs) is supported, not DRI2 (window system makes the winys45* BOs).46*/4748#ifdef USE_VC4_SIMULATOR4950#include <sys/mman.h>51#include "xf86drm.h"52#include "util/u_memory.h"53#include "util/u_mm.h"54#include "util/ralloc.h"5556#include "vc4_screen.h"57#include "vc4_cl_dump.h"58#include "vc4_context.h"59#include "kernel/vc4_drv.h"60#include "vc4_simulator_validate.h"61#include "simpenrose/simpenrose.h"6263/** Global (across GEM fds) state for the simulator */64static struct vc4_simulator_state {65mtx_t mutex;6667void *mem;68ssize_t mem_size;69struct mem_block *heap;70struct mem_block *overflow;7172/** Mapping from GEM handle to struct vc4_simulator_bo * */73struct hash_table *fd_map;7475int refcount;76} sim_state = {77.mutex = _MTX_INITIALIZER_NP,78};7980/** Per-GEM-fd state for the simulator. */81struct vc4_simulator_file {82int fd;8384/* This is weird -- we make a "vc4_device" per file, even though on85* the kernel side this is a global. We do this so that kernel code86* calling us for BO allocation can get to our screen.87*/88struct drm_device dev;8990/** Mapping from GEM handle to struct vc4_simulator_bo * */91struct hash_table *bo_map;92};9394/** Wrapper for drm_vc4_bo tracking the simulator-specific state. */95struct vc4_simulator_bo {96struct drm_vc4_bo base;97struct vc4_simulator_file *file;9899/** Area for this BO within sim_state->mem */100struct mem_block *block;101102int handle;103104/* Mapping of the underlying GEM object that we copy in/out of105* simulator memory.106*/107void *gem_vaddr;108};109110static void *111int_to_key(int key)112{113return (void *)(uintptr_t)key;114}115116static struct vc4_simulator_file *117vc4_get_simulator_file_for_fd(int fd)118{119struct hash_entry *entry = _mesa_hash_table_search(sim_state.fd_map,120int_to_key(fd + 1));121return entry ? entry->data : NULL;122}123124/* A marker placed just after each BO, then checked after rendering to make125* sure it's still there.126*/127#define BO_SENTINEL 0xfedcba98128129#define PAGE_ALIGN2 12130131/**132* Allocates space in simulator memory and returns a tracking struct for it133* that also contains the drm_gem_cma_object struct.134*/135static struct vc4_simulator_bo *136vc4_create_simulator_bo(int fd, int handle, unsigned size)137{138struct vc4_simulator_file *file = vc4_get_simulator_file_for_fd(fd);139struct vc4_simulator_bo *sim_bo = rzalloc(file,140struct vc4_simulator_bo);141struct drm_vc4_bo *bo = &sim_bo->base;142struct drm_gem_cma_object *obj = &bo->base;143size = align(size, 4096);144145sim_bo->file = file;146sim_bo->handle = handle;147148/* Allocate space for the buffer in simulator memory. */149mtx_lock(&sim_state.mutex);150sim_bo->block = u_mmAllocMem(sim_state.heap, size + 4, PAGE_ALIGN2, 0);151mtx_unlock(&sim_state.mutex);152assert(sim_bo->block);153154obj->base.size = size;155obj->base.dev = &file->dev;156obj->vaddr = sim_state.mem + sim_bo->block->ofs;157obj->paddr = simpenrose_hw_addr(obj->vaddr);158159*(uint32_t *)(obj->vaddr + size) = BO_SENTINEL;160161/* A handle of 0 is used for vc4_gem.c internal allocations that162* don't need to go in the lookup table.163*/164if (handle != 0) {165mtx_lock(&sim_state.mutex);166_mesa_hash_table_insert(file->bo_map, int_to_key(handle), bo);167mtx_unlock(&sim_state.mutex);168169/* Map the GEM buffer for copy in/out to the simulator. */170struct drm_mode_map_dumb map = {171.handle = handle,172};173int ret = drmIoctl(fd, DRM_IOCTL_MODE_MAP_DUMB, &map);174if (ret) {175fprintf(stderr, "Failed to get MMAP offset: %d\n",176errno);177abort();178}179sim_bo->gem_vaddr = mmap(NULL, obj->base.size,180PROT_READ | PROT_WRITE, MAP_SHARED,181fd, map.offset);182if (sim_bo->gem_vaddr == MAP_FAILED) {183fprintf(stderr, "mmap of bo %d (offset 0x%016llx, size %d) failed\n",184handle, (long long)map.offset, (int)obj->base.size);185abort();186}187}188189return sim_bo;190}191192static void193vc4_free_simulator_bo(struct vc4_simulator_bo *sim_bo)194{195struct vc4_simulator_file *sim_file = sim_bo->file;196struct drm_vc4_bo *bo = &sim_bo->base;197struct drm_gem_cma_object *obj = &bo->base;198199if (bo->validated_shader) {200free(bo->validated_shader->texture_samples);201free(bo->validated_shader);202}203204if (sim_bo->gem_vaddr)205munmap(sim_bo->gem_vaddr, obj->base.size);206207mtx_lock(&sim_state.mutex);208u_mmFreeMem(sim_bo->block);209if (sim_bo->handle) {210_mesa_hash_table_remove_key(sim_file->bo_map,211int_to_key(sim_bo->handle));212}213mtx_unlock(&sim_state.mutex);214ralloc_free(sim_bo);215}216217static struct vc4_simulator_bo *218vc4_get_simulator_bo(struct vc4_simulator_file *file, int gem_handle)219{220mtx_lock(&sim_state.mutex);221struct hash_entry *entry =222_mesa_hash_table_search(file->bo_map, int_to_key(gem_handle));223mtx_unlock(&sim_state.mutex);224225return entry ? entry->data : NULL;226}227228struct drm_gem_cma_object *229drm_gem_cma_create(struct drm_device *dev, size_t size)230{231struct vc4_screen *screen = dev->screen;232struct vc4_simulator_bo *sim_bo = vc4_create_simulator_bo(screen->fd,2330, size);234return &sim_bo->base.base;235}236237static int238vc4_simulator_pin_bos(struct vc4_simulator_file *file,239struct vc4_exec_info *exec)240{241struct drm_vc4_submit_cl *args = exec->args;242uint32_t *bo_handles = (uint32_t *)(uintptr_t)args->bo_handles;243244exec->bo_count = args->bo_handle_count;245exec->bo = calloc(exec->bo_count, sizeof(void *));246for (int i = 0; i < exec->bo_count; i++) {247struct vc4_simulator_bo *sim_bo =248vc4_get_simulator_bo(file, bo_handles[i]);249struct drm_vc4_bo *drm_bo = &sim_bo->base;250struct drm_gem_cma_object *obj = &drm_bo->base;251252memcpy(obj->vaddr, sim_bo->gem_vaddr, obj->base.size);253254exec->bo[i] = obj;255}256return 0;257}258259static int260vc4_simulator_unpin_bos(struct vc4_exec_info *exec)261{262for (int i = 0; i < exec->bo_count; i++) {263struct drm_gem_cma_object *obj = exec->bo[i];264struct drm_vc4_bo *drm_bo = to_vc4_bo(&obj->base);265struct vc4_simulator_bo *sim_bo =266(struct vc4_simulator_bo *)drm_bo;267268assert(*(uint32_t *)(obj->vaddr +269obj->base.size) == BO_SENTINEL);270if (sim_bo->gem_vaddr)271memcpy(sim_bo->gem_vaddr, obj->vaddr, obj->base.size);272}273274free(exec->bo);275276return 0;277}278279static void280vc4_dump_to_file(struct vc4_exec_info *exec)281{282static int dumpno = 0;283struct drm_vc4_get_hang_state *state;284struct drm_vc4_get_hang_state_bo *bo_state;285unsigned int dump_version = 0;286287if (!(vc4_debug & VC4_DEBUG_DUMP))288return;289290state = calloc(1, sizeof(*state));291292int unref_count = 0;293list_for_each_entry_safe(struct drm_vc4_bo, bo, &exec->unref_list,294unref_head) {295unref_count++;296}297298/* Add one more for the overflow area that isn't wrapped in a BO. */299state->bo_count = exec->bo_count + unref_count + 1;300bo_state = calloc(state->bo_count, sizeof(*bo_state));301302char *filename = NULL;303asprintf(&filename, "vc4-dri-%d.dump", dumpno++);304FILE *f = fopen(filename, "w+");305if (!f) {306fprintf(stderr, "Couldn't open %s: %s", filename,307strerror(errno));308return;309}310311fwrite(&dump_version, sizeof(dump_version), 1, f);312313state->ct0ca = exec->ct0ca;314state->ct0ea = exec->ct0ea;315state->ct1ca = exec->ct1ca;316state->ct1ea = exec->ct1ea;317state->start_bin = exec->ct0ca;318state->start_render = exec->ct1ca;319fwrite(state, sizeof(*state), 1, f);320321int i;322for (i = 0; i < exec->bo_count; i++) {323struct drm_gem_cma_object *cma_bo = exec->bo[i];324bo_state[i].handle = i; /* Not used by the parser. */325bo_state[i].paddr = cma_bo->paddr;326bo_state[i].size = cma_bo->base.size;327}328329list_for_each_entry_safe(struct drm_vc4_bo, bo, &exec->unref_list,330unref_head) {331struct drm_gem_cma_object *cma_bo = &bo->base;332bo_state[i].handle = 0;333bo_state[i].paddr = cma_bo->paddr;334bo_state[i].size = cma_bo->base.size;335i++;336}337338/* Add the static overflow memory area. */339bo_state[i].handle = exec->bo_count;340bo_state[i].paddr = sim_state.overflow->ofs;341bo_state[i].size = sim_state.overflow->size;342i++;343344fwrite(bo_state, sizeof(*bo_state), state->bo_count, f);345346for (int i = 0; i < exec->bo_count; i++) {347struct drm_gem_cma_object *cma_bo = exec->bo[i];348fwrite(cma_bo->vaddr, cma_bo->base.size, 1, f);349}350351list_for_each_entry_safe(struct drm_vc4_bo, bo, &exec->unref_list,352unref_head) {353struct drm_gem_cma_object *cma_bo = &bo->base;354fwrite(cma_bo->vaddr, cma_bo->base.size, 1, f);355}356357void *overflow = calloc(1, sim_state.overflow->size);358fwrite(overflow, 1, sim_state.overflow->size, f);359free(overflow);360361free(state);362free(bo_state);363fclose(f);364}365366static int367vc4_simulator_submit_cl_ioctl(int fd, struct drm_vc4_submit_cl *args)368{369struct vc4_simulator_file *file = vc4_get_simulator_file_for_fd(fd);370struct vc4_exec_info exec;371struct drm_device *dev = &file->dev;372int ret;373374memset(&exec, 0, sizeof(exec));375list_inithead(&exec.unref_list);376377exec.args = args;378379ret = vc4_simulator_pin_bos(file, &exec);380if (ret)381return ret;382383ret = vc4_cl_validate(dev, &exec);384if (ret)385return ret;386387if (vc4_debug & VC4_DEBUG_CL) {388fprintf(stderr, "RCL:\n");389vc4_dump_cl(sim_state.mem + exec.ct1ca,390exec.ct1ea - exec.ct1ca, true);391}392393vc4_dump_to_file(&exec);394395if (exec.ct0ca != exec.ct0ea) {396int bfc = simpenrose_do_binning(exec.ct0ca, exec.ct0ea);397if (bfc != 1) {398fprintf(stderr, "Binning returned %d flushes, should be 1.\n",399bfc);400fprintf(stderr, "Relocated binning command list:\n");401vc4_dump_cl(sim_state.mem + exec.ct0ca,402exec.ct0ea - exec.ct0ca, false);403abort();404}405}406int rfc = simpenrose_do_rendering(exec.ct1ca, exec.ct1ea);407if (rfc != 1) {408fprintf(stderr, "Rendering returned %d frames, should be 1.\n",409rfc);410fprintf(stderr, "Relocated render command list:\n");411vc4_dump_cl(sim_state.mem + exec.ct1ca,412exec.ct1ea - exec.ct1ca, true);413abort();414}415416ret = vc4_simulator_unpin_bos(&exec);417if (ret)418return ret;419420list_for_each_entry_safe(struct drm_vc4_bo, bo, &exec.unref_list,421unref_head) {422struct vc4_simulator_bo *sim_bo = (struct vc4_simulator_bo *)bo;423ASSERTED struct drm_gem_cma_object *obj = &sim_bo->base.base;424list_del(&bo->unref_head);425assert(*(uint32_t *)(obj->vaddr + obj->base.size) ==426BO_SENTINEL);427vc4_free_simulator_bo(sim_bo);428}429430return 0;431}432433/**434* Do fixups after a BO has been opened from a handle.435*436* This could be done at DRM_IOCTL_GEM_OPEN/DRM_IOCTL_GEM_PRIME_FD_TO_HANDLE437* time, but we're still using drmPrimeFDToHandle() so we have this helper to438* be called afterward instead.439*/440void vc4_simulator_open_from_handle(int fd, int handle, uint32_t size)441{442vc4_create_simulator_bo(fd, handle, size);443}444445/**446* Simulated ioctl(fd, DRM_VC4_CREATE_BO) implementation.447*448* Making a VC4 BO is just a matter of making a corresponding BO on the host.449*/450static int451vc4_simulator_create_bo_ioctl(int fd, struct drm_vc4_create_bo *args)452{453int ret;454struct drm_mode_create_dumb create = {455.width = 128,456.bpp = 8,457.height = (args->size + 127) / 128,458};459460ret = drmIoctl(fd, DRM_IOCTL_MODE_CREATE_DUMB, &create);461assert(create.size >= args->size);462463args->handle = create.handle;464465vc4_create_simulator_bo(fd, create.handle, args->size);466467return ret;468}469470/**471* Simulated ioctl(fd, DRM_VC4_CREATE_SHADER_BO) implementation.472*473* In simulation we defer shader validation until exec time. Just make a host474* BO and memcpy the contents in.475*/476static int477vc4_simulator_create_shader_bo_ioctl(int fd,478struct drm_vc4_create_shader_bo *args)479{480int ret;481struct drm_mode_create_dumb create = {482.width = 128,483.bpp = 8,484.height = (args->size + 127) / 128,485};486487ret = drmIoctl(fd, DRM_IOCTL_MODE_CREATE_DUMB, &create);488if (ret)489return ret;490assert(create.size >= args->size);491492args->handle = create.handle;493494struct vc4_simulator_bo *sim_bo =495vc4_create_simulator_bo(fd, create.handle, args->size);496struct drm_vc4_bo *drm_bo = &sim_bo->base;497struct drm_gem_cma_object *obj = &drm_bo->base;498499/* Copy into the simulator's BO for validation. */500memcpy(obj->vaddr, (void *)(uintptr_t)args->data, args->size);501502/* Copy into the GEM BO to prevent the simulator_pin_bos() from503* smashing it.504*/505memcpy(sim_bo->gem_vaddr, (void *)(uintptr_t)args->data, args->size);506507drm_bo->validated_shader = vc4_validate_shader(obj);508if (!drm_bo->validated_shader)509return -EINVAL;510511return 0;512}513514/**515* Simulated ioctl(fd, DRM_VC4_MMAP_BO) implementation.516*517* We just pass this straight through to dumb mmap.518*/519static int520vc4_simulator_mmap_bo_ioctl(int fd, struct drm_vc4_mmap_bo *args)521{522int ret;523struct drm_mode_map_dumb map = {524.handle = args->handle,525};526527ret = drmIoctl(fd, DRM_IOCTL_MODE_MAP_DUMB, &map);528args->offset = map.offset;529530return ret;531}532533static int534vc4_simulator_gem_close_ioctl(int fd, struct drm_gem_close *args)535{536/* Free the simulator's internal tracking. */537struct vc4_simulator_file *file = vc4_get_simulator_file_for_fd(fd);538struct vc4_simulator_bo *sim_bo = vc4_get_simulator_bo(file,539args->handle);540541vc4_free_simulator_bo(sim_bo);542543/* Pass the call on down. */544return drmIoctl(fd, DRM_IOCTL_GEM_CLOSE, args);545}546547static int548vc4_simulator_get_param_ioctl(int fd, struct drm_vc4_get_param *args)549{550switch (args->param) {551case DRM_VC4_PARAM_SUPPORTS_BRANCHES:552case DRM_VC4_PARAM_SUPPORTS_ETC1:553case DRM_VC4_PARAM_SUPPORTS_THREADED_FS:554case DRM_VC4_PARAM_SUPPORTS_FIXED_RCL_ORDER:555args->value = true;556return 0;557558case DRM_VC4_PARAM_SUPPORTS_MADVISE:559case DRM_VC4_PARAM_SUPPORTS_PERFMON:560errno = -EINVAL;561return -1;562563case DRM_VC4_PARAM_V3D_IDENT0:564args->value = 0x02000000;565return 0;566567case DRM_VC4_PARAM_V3D_IDENT1:568args->value = 0x00000001;569return 0;570571default:572fprintf(stderr, "Unknown DRM_IOCTL_VC4_GET_PARAM(%lld)\n",573(long long)args->param);574abort();575};576}577578int579vc4_simulator_ioctl(int fd, unsigned long request, void *args)580{581switch (request) {582case DRM_IOCTL_VC4_SUBMIT_CL:583return vc4_simulator_submit_cl_ioctl(fd, args);584case DRM_IOCTL_VC4_CREATE_BO:585return vc4_simulator_create_bo_ioctl(fd, args);586case DRM_IOCTL_VC4_CREATE_SHADER_BO:587return vc4_simulator_create_shader_bo_ioctl(fd, args);588case DRM_IOCTL_VC4_MMAP_BO:589return vc4_simulator_mmap_bo_ioctl(fd, args);590591case DRM_IOCTL_VC4_WAIT_BO:592case DRM_IOCTL_VC4_WAIT_SEQNO:593/* We do all of the vc4 rendering synchronously, so we just594* return immediately on the wait ioctls. This ignores any595* native rendering to the host BO, so it does mean we race on596* front buffer rendering.597*/598return 0;599600case DRM_IOCTL_VC4_LABEL_BO:601/* This is just debug information, nothing to do. */602return 0;603604case DRM_IOCTL_VC4_GET_TILING:605case DRM_IOCTL_VC4_SET_TILING:606/* Disable these for now, since the sharing with i965 requires607* linear buffers.608*/609errno = -EINVAL;610return -1;611612case DRM_IOCTL_VC4_GET_PARAM:613return vc4_simulator_get_param_ioctl(fd, args);614615case DRM_IOCTL_GEM_CLOSE:616return vc4_simulator_gem_close_ioctl(fd, args);617618case DRM_IOCTL_GEM_OPEN:619case DRM_IOCTL_GEM_FLINK:620return drmIoctl(fd, request, args);621default:622fprintf(stderr, "Unknown ioctl 0x%08x\n", (int)request);623abort();624}625}626627static void628vc4_simulator_init_global(void)629{630mtx_lock(&sim_state.mutex);631if (sim_state.refcount++) {632mtx_unlock(&sim_state.mutex);633return;634}635636sim_state.mem_size = 256 * 1024 * 1024;637sim_state.mem = calloc(sim_state.mem_size, 1);638if (!sim_state.mem)639abort();640sim_state.heap = u_mmInit(0, sim_state.mem_size);641642/* We supply our own memory so that we can have more aperture643* available (256MB instead of simpenrose's default 64MB).644*/645simpenrose_init_hardware_supply_mem(sim_state.mem, sim_state.mem_size);646647/* Carve out low memory for tile allocation overflow. The kernel648* should be automatically handling overflow memory setup on real649* hardware, but for simulation we just get one shot to set up enough650* overflow memory before execution. This overflow mem will be used651* up over the whole lifetime of simpenrose (not reused on each652* flush), so it had better be big.653*/654sim_state.overflow = u_mmAllocMem(sim_state.heap, 32 * 1024 * 1024,655PAGE_ALIGN2, 0);656simpenrose_supply_overflow_mem(sim_state.overflow->ofs,657sim_state.overflow->size);658659mtx_unlock(&sim_state.mutex);660661sim_state.fd_map =662_mesa_hash_table_create(NULL,663_mesa_hash_pointer,664_mesa_key_pointer_equal);665}666667void668vc4_simulator_init(struct vc4_screen *screen)669{670vc4_simulator_init_global();671672screen->sim_file = rzalloc(screen, struct vc4_simulator_file);673674screen->sim_file->bo_map =675_mesa_hash_table_create(screen->sim_file,676_mesa_hash_pointer,677_mesa_key_pointer_equal);678679mtx_lock(&sim_state.mutex);680_mesa_hash_table_insert(sim_state.fd_map, int_to_key(screen->fd + 1),681screen->sim_file);682mtx_unlock(&sim_state.mutex);683684screen->sim_file->dev.screen = screen;685}686687void688vc4_simulator_destroy(struct vc4_screen *screen)689{690mtx_lock(&sim_state.mutex);691if (!--sim_state.refcount) {692_mesa_hash_table_destroy(sim_state.fd_map, NULL);693u_mmDestroy(sim_state.heap);694free(sim_state.mem);695/* No memsetting it, because it contains the mutex. */696}697mtx_unlock(&sim_state.mutex);698}699700#endif /* USE_VC4_SIMULATOR */701702703