Path: blob/21.2-virgl/src/broadcom/simulator/v3d_simulator.c
4560 views
/*1* Copyright © 2014-2017 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 v3d_simulator.c25*26* Implements V3D simulation on top of a non-V3D GEM fd.27*28* This file's goal is to emulate the V3D ioctls' behavior in the kernel on29* top of the simpenrose software simulator. Generally, V3D 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 V3D is going to use, which of course43* doesn't work out in practice. This means that for now, only DRI3 (V3D44* makes the winsys BOs) is supported, not DRI2 (window system makes the winys45* BOs).46*/4748#ifdef USE_V3D_SIMULATOR4950#include <stdio.h>51#include <sys/mman.h>52#include "c11/threads.h"53#include "util/hash_table.h"54#include "util/ralloc.h"55#include "util/set.h"56#include "util/u_dynarray.h"57#include "util/u_memory.h"58#include "util/u_mm.h"59#include "util/u_math.h"6061#include <xf86drm.h>62#include "drm-uapi/i915_drm.h"63#include "drm-uapi/v3d_drm.h"6465#include "v3d_simulator.h"66#include "v3d_simulator_wrapper.h"6768/** Global (across GEM fds) state for the simulator */69static struct v3d_simulator_state {70mtx_t mutex;71mtx_t submit_lock;7273struct v3d_hw *v3d;74int ver;7576/* Base virtual address of the heap. */77void *mem;78/* Base hardware address of the heap. */79uint32_t mem_base;80/* Size of the heap. */81uint32_t mem_size;8283struct mem_block *heap;84struct mem_block *overflow;8586/** Mapping from GEM fd to struct v3d_simulator_file * */87struct hash_table *fd_map;8889struct util_dynarray bin_oom;90int refcount;91} sim_state = {92.mutex = _MTX_INITIALIZER_NP,93};9495/** Per-GEM-fd state for the simulator. */96struct v3d_simulator_file {97int fd;9899/** Mapping from GEM handle to struct v3d_simulator_bo * */100struct hash_table *bo_map;101102struct mem_block *gmp;103void *gmp_vaddr;104105/** Actual GEM fd is i915, so we should use their create ioctl. */106bool is_i915;107};108109/** Wrapper for drm_v3d_bo tracking the simulator-specific state. */110struct v3d_simulator_bo {111struct v3d_simulator_file *file;112113/** Area for this BO within sim_state->mem */114struct mem_block *block;115uint32_t size;116uint64_t mmap_offset;117void *sim_vaddr;118void *gem_vaddr;119120int handle;121};122123static void *124int_to_key(int key)125{126return (void *)(uintptr_t)key;127}128129static struct v3d_simulator_file *130v3d_get_simulator_file_for_fd(int fd)131{132struct hash_entry *entry = _mesa_hash_table_search(sim_state.fd_map,133int_to_key(fd + 1));134return entry ? entry->data : NULL;135}136137/* A marker placed just after each BO, then checked after rendering to make138* sure it's still there.139*/140#define BO_SENTINEL 0xfedcba98141142/* 128kb */143#define GMP_ALIGN2 17144145/**146* Sets the range of GPU virtual address space to have the given GMP147* permissions (bit 0 = read, bit 1 = write, write-only forbidden).148*/149static void150set_gmp_flags(struct v3d_simulator_file *file,151uint32_t offset, uint32_t size, uint32_t flag)152{153assert((offset & ((1 << GMP_ALIGN2) - 1)) == 0);154int gmp_offset = offset >> GMP_ALIGN2;155int gmp_count = align(size, 1 << GMP_ALIGN2) >> GMP_ALIGN2;156uint32_t *gmp = file->gmp_vaddr;157158assert(flag <= 0x3);159160for (int i = gmp_offset; i < gmp_offset + gmp_count; i++) {161int32_t bitshift = (i % 16) * 2;162gmp[i / 16] &= ~(0x3 << bitshift);163gmp[i / 16] |= flag << bitshift;164}165}166167/**168* Allocates space in simulator memory and returns a tracking struct for it169* that also contains the drm_gem_cma_object struct.170*/171static struct v3d_simulator_bo *172v3d_create_simulator_bo(int fd, unsigned size)173{174struct v3d_simulator_file *file = v3d_get_simulator_file_for_fd(fd);175struct v3d_simulator_bo *sim_bo = rzalloc(file,176struct v3d_simulator_bo);177size = align(size, 4096);178179sim_bo->file = file;180181mtx_lock(&sim_state.mutex);182sim_bo->block = u_mmAllocMem(sim_state.heap, size + 4, GMP_ALIGN2, 0);183mtx_unlock(&sim_state.mutex);184assert(sim_bo->block);185186set_gmp_flags(file, sim_bo->block->ofs, size, 0x3);187188sim_bo->size = size;189190/* Allocate space for the buffer in simulator memory. */191sim_bo->sim_vaddr = sim_state.mem + sim_bo->block->ofs - sim_state.mem_base;192memset(sim_bo->sim_vaddr, 0xd0, size);193194*(uint32_t *)(sim_bo->sim_vaddr + sim_bo->size) = BO_SENTINEL;195196return sim_bo;197}198199static struct v3d_simulator_bo *200v3d_create_simulator_bo_for_gem(int fd, int handle, unsigned size)201{202struct v3d_simulator_file *file = v3d_get_simulator_file_for_fd(fd);203struct v3d_simulator_bo *sim_bo =204v3d_create_simulator_bo(fd, size);205206sim_bo->handle = handle;207208/* Map the GEM buffer for copy in/out to the simulator. i915 blocks209* dumb mmap on render nodes, so use their ioctl directly if we're on210* one.211*/212int ret;213if (file->is_i915) {214struct drm_i915_gem_mmap_gtt map = {215.handle = handle,216};217218/* We could potentially use non-gtt (cached) for LLC systems,219* but the copy-in/out won't be the limiting factor on220* simulation anyway.221*/222ret = drmIoctl(fd, DRM_IOCTL_I915_GEM_MMAP_GTT, &map);223sim_bo->mmap_offset = map.offset;224} else {225struct drm_mode_map_dumb map = {226.handle = handle,227};228229ret = drmIoctl(fd, DRM_IOCTL_MODE_MAP_DUMB, &map);230sim_bo->mmap_offset = map.offset;231}232if (ret) {233fprintf(stderr, "Failed to get MMAP offset: %d\n", ret);234abort();235}236237sim_bo->gem_vaddr = mmap(NULL, sim_bo->size,238PROT_READ | PROT_WRITE, MAP_SHARED,239fd, sim_bo->mmap_offset);240if (sim_bo->gem_vaddr == MAP_FAILED) {241fprintf(stderr, "mmap of bo %d (offset 0x%016llx, size %d) failed\n",242handle, (long long)sim_bo->mmap_offset, sim_bo->size);243abort();244}245246/* A handle of 0 is used for v3d_gem.c internal allocations that247* don't need to go in the lookup table.248*/249if (handle != 0) {250mtx_lock(&sim_state.mutex);251_mesa_hash_table_insert(file->bo_map, int_to_key(handle),252sim_bo);253mtx_unlock(&sim_state.mutex);254}255256return sim_bo;257}258259static int bin_fd;260261uint32_t262v3d_simulator_get_spill(uint32_t spill_size)263{264struct v3d_simulator_bo *sim_bo =265v3d_create_simulator_bo(bin_fd, spill_size);266267util_dynarray_append(&sim_state.bin_oom, struct v3d_simulator_bo *,268sim_bo);269270return sim_bo->block->ofs;271}272273static void274v3d_free_simulator_bo(struct v3d_simulator_bo *sim_bo)275{276struct v3d_simulator_file *sim_file = sim_bo->file;277278set_gmp_flags(sim_file, sim_bo->block->ofs, sim_bo->size, 0x0);279280if (sim_bo->gem_vaddr)281munmap(sim_bo->gem_vaddr, sim_bo->size);282283mtx_lock(&sim_state.mutex);284u_mmFreeMem(sim_bo->block);285if (sim_bo->handle) {286_mesa_hash_table_remove_key(sim_file->bo_map,287int_to_key(sim_bo->handle));288}289mtx_unlock(&sim_state.mutex);290ralloc_free(sim_bo);291}292293static struct v3d_simulator_bo *294v3d_get_simulator_bo(struct v3d_simulator_file *file, int gem_handle)295{296if (gem_handle == 0)297return NULL;298299mtx_lock(&sim_state.mutex);300struct hash_entry *entry =301_mesa_hash_table_search(file->bo_map, int_to_key(gem_handle));302mtx_unlock(&sim_state.mutex);303304return entry ? entry->data : NULL;305}306307static void308v3d_simulator_copy_in_handle(struct v3d_simulator_file *file, int handle)309{310struct v3d_simulator_bo *sim_bo = v3d_get_simulator_bo(file, handle);311312if (!sim_bo)313return;314315memcpy(sim_bo->sim_vaddr, sim_bo->gem_vaddr, sim_bo->size);316}317318static void319v3d_simulator_copy_out_handle(struct v3d_simulator_file *file, int handle)320{321struct v3d_simulator_bo *sim_bo = v3d_get_simulator_bo(file, handle);322323if (!sim_bo)324return;325326memcpy(sim_bo->gem_vaddr, sim_bo->sim_vaddr, sim_bo->size);327328if (*(uint32_t *)(sim_bo->sim_vaddr +329sim_bo->size) != BO_SENTINEL) {330fprintf(stderr, "Buffer overflow in handle %d\n",331handle);332}333}334335static int336v3d_simulator_pin_bos(struct v3d_simulator_file *file,337struct drm_v3d_submit_cl *submit)338{339uint32_t *bo_handles = (uint32_t *)(uintptr_t)submit->bo_handles;340341for (int i = 0; i < submit->bo_handle_count; i++)342v3d_simulator_copy_in_handle(file, bo_handles[i]);343344return 0;345}346347static int348v3d_simulator_unpin_bos(struct v3d_simulator_file *file,349struct drm_v3d_submit_cl *submit)350{351uint32_t *bo_handles = (uint32_t *)(uintptr_t)submit->bo_handles;352353for (int i = 0; i < submit->bo_handle_count; i++)354v3d_simulator_copy_out_handle(file, bo_handles[i]);355356return 0;357}358359static int360v3d_simulator_submit_cl_ioctl(int fd, struct drm_v3d_submit_cl *submit)361{362struct v3d_simulator_file *file = v3d_get_simulator_file_for_fd(fd);363int ret;364365ret = v3d_simulator_pin_bos(file, submit);366if (ret)367return ret;368369mtx_lock(&sim_state.submit_lock);370bin_fd = fd;371if (sim_state.ver >= 41)372v3d41_simulator_submit_cl_ioctl(sim_state.v3d, submit, file->gmp->ofs);373else374v3d33_simulator_submit_cl_ioctl(sim_state.v3d, submit, file->gmp->ofs);375376util_dynarray_foreach(&sim_state.bin_oom, struct v3d_simulator_bo *,377sim_bo) {378v3d_free_simulator_bo(*sim_bo);379}380util_dynarray_clear(&sim_state.bin_oom);381382mtx_unlock(&sim_state.submit_lock);383384ret = v3d_simulator_unpin_bos(file, submit);385if (ret)386return ret;387388return 0;389}390391/**392* Do fixups after a BO has been opened from a handle.393*394* This could be done at DRM_IOCTL_GEM_OPEN/DRM_IOCTL_GEM_PRIME_FD_TO_HANDLE395* time, but we're still using drmPrimeFDToHandle() so we have this helper to396* be called afterward instead.397*/398void v3d_simulator_open_from_handle(int fd, int handle, uint32_t size)399{400v3d_create_simulator_bo_for_gem(fd, handle, size);401}402403/**404* Simulated ioctl(fd, DRM_V3D_CREATE_BO) implementation.405*406* Making a V3D BO is just a matter of making a corresponding BO on the host.407*/408static int409v3d_simulator_create_bo_ioctl(int fd, struct drm_v3d_create_bo *args)410{411struct v3d_simulator_file *file = v3d_get_simulator_file_for_fd(fd);412413/* i915 bans dumb create on render nodes, so we have to use their414* native ioctl in case we're on a render node.415*/416int ret;417if (file->is_i915) {418struct drm_i915_gem_create create = {419.size = args->size,420};421ret = drmIoctl(fd, DRM_IOCTL_I915_GEM_CREATE, &create);422423args->handle = create.handle;424} else {425struct drm_mode_create_dumb create = {426.width = 128,427.bpp = 8,428.height = (args->size + 127) / 128,429};430431ret = drmIoctl(fd, DRM_IOCTL_MODE_CREATE_DUMB, &create);432assert(ret != 0 || create.size >= args->size);433434args->handle = create.handle;435}436437if (ret == 0) {438struct v3d_simulator_bo *sim_bo =439v3d_create_simulator_bo_for_gem(fd, args->handle,440args->size);441442args->offset = sim_bo->block->ofs;443}444445return ret;446}447448/**449* Simulated ioctl(fd, DRM_V3D_MMAP_BO) implementation.450*451* We've already grabbed the mmap offset when we created the sim bo, so just452* return it.453*/454static int455v3d_simulator_mmap_bo_ioctl(int fd, struct drm_v3d_mmap_bo *args)456{457struct v3d_simulator_file *file = v3d_get_simulator_file_for_fd(fd);458struct v3d_simulator_bo *sim_bo = v3d_get_simulator_bo(file,459args->handle);460461args->offset = sim_bo->mmap_offset;462463return 0;464}465466static int467v3d_simulator_get_bo_offset_ioctl(int fd, struct drm_v3d_get_bo_offset *args)468{469struct v3d_simulator_file *file = v3d_get_simulator_file_for_fd(fd);470struct v3d_simulator_bo *sim_bo = v3d_get_simulator_bo(file,471args->handle);472473args->offset = sim_bo->block->ofs;474475return 0;476}477478static int479v3d_simulator_gem_close_ioctl(int fd, struct drm_gem_close *args)480{481/* Free the simulator's internal tracking. */482struct v3d_simulator_file *file = v3d_get_simulator_file_for_fd(fd);483struct v3d_simulator_bo *sim_bo = v3d_get_simulator_bo(file,484args->handle);485486v3d_free_simulator_bo(sim_bo);487488/* Pass the call on down. */489return drmIoctl(fd, DRM_IOCTL_GEM_CLOSE, args);490}491492static int493v3d_simulator_get_param_ioctl(int fd, struct drm_v3d_get_param *args)494{495if (sim_state.ver >= 41)496return v3d41_simulator_get_param_ioctl(sim_state.v3d, args);497else498return v3d33_simulator_get_param_ioctl(sim_state.v3d, args);499}500501static int502v3d_simulator_submit_tfu_ioctl(int fd, struct drm_v3d_submit_tfu *args)503{504struct v3d_simulator_file *file = v3d_get_simulator_file_for_fd(fd);505int ret;506507v3d_simulator_copy_in_handle(file, args->bo_handles[0]);508v3d_simulator_copy_in_handle(file, args->bo_handles[1]);509v3d_simulator_copy_in_handle(file, args->bo_handles[2]);510v3d_simulator_copy_in_handle(file, args->bo_handles[3]);511512if (sim_state.ver >= 41)513ret = v3d41_simulator_submit_tfu_ioctl(sim_state.v3d, args);514else515ret = v3d33_simulator_submit_tfu_ioctl(sim_state.v3d, args);516517v3d_simulator_copy_out_handle(file, args->bo_handles[0]);518519return ret;520}521522static int523v3d_simulator_submit_csd_ioctl(int fd, struct drm_v3d_submit_csd *args)524{525struct v3d_simulator_file *file = v3d_get_simulator_file_for_fd(fd);526uint32_t *bo_handles = (uint32_t *)(uintptr_t)args->bo_handles;527int ret;528529for (int i = 0; i < args->bo_handle_count; i++)530v3d_simulator_copy_in_handle(file, bo_handles[i]);531532if (sim_state.ver >= 41)533ret = v3d41_simulator_submit_csd_ioctl(sim_state.v3d, args,534file->gmp->ofs);535else536ret = -1;537538for (int i = 0; i < args->bo_handle_count; i++)539v3d_simulator_copy_out_handle(file, bo_handles[i]);540541return ret;542}543544int545v3d_simulator_ioctl(int fd, unsigned long request, void *args)546{547switch (request) {548case DRM_IOCTL_V3D_SUBMIT_CL:549return v3d_simulator_submit_cl_ioctl(fd, args);550case DRM_IOCTL_V3D_CREATE_BO:551return v3d_simulator_create_bo_ioctl(fd, args);552case DRM_IOCTL_V3D_MMAP_BO:553return v3d_simulator_mmap_bo_ioctl(fd, args);554case DRM_IOCTL_V3D_GET_BO_OFFSET:555return v3d_simulator_get_bo_offset_ioctl(fd, args);556557case DRM_IOCTL_V3D_WAIT_BO:558/* We do all of the v3d rendering synchronously, so we just559* return immediately on the wait ioctls. This ignores any560* native rendering to the host BO, so it does mean we race on561* front buffer rendering.562*/563return 0;564565case DRM_IOCTL_V3D_GET_PARAM:566return v3d_simulator_get_param_ioctl(fd, args);567568case DRM_IOCTL_GEM_CLOSE:569return v3d_simulator_gem_close_ioctl(fd, args);570571case DRM_IOCTL_V3D_SUBMIT_TFU:572return v3d_simulator_submit_tfu_ioctl(fd, args);573574case DRM_IOCTL_V3D_SUBMIT_CSD:575return v3d_simulator_submit_csd_ioctl(fd, args);576577case DRM_IOCTL_GEM_OPEN:578case DRM_IOCTL_GEM_FLINK:579return drmIoctl(fd, request, args);580default:581fprintf(stderr, "Unknown ioctl 0x%08x\n", (int)request);582abort();583}584}585586uint32_t587v3d_simulator_get_mem_size(void)588{589return sim_state.mem_size;590}591592static void593v3d_simulator_init_global()594{595mtx_lock(&sim_state.mutex);596if (sim_state.refcount++) {597mtx_unlock(&sim_state.mutex);598return;599}600601sim_state.v3d = v3d_hw_auto_new(NULL);602v3d_hw_alloc_mem(sim_state.v3d, 1024 * 1024 * 1024);603sim_state.mem_base =604v3d_hw_get_mem(sim_state.v3d, &sim_state.mem_size,605&sim_state.mem);606607/* Allocate from anywhere from 4096 up. We don't allocate at 0,608* because for OQs and some other addresses in the HW, 0 means609* disabled.610*/611sim_state.heap = u_mmInit(4096, sim_state.mem_size - 4096);612613/* Make a block of 0xd0 at address 0 to make sure we don't screw up614* and land there.615*/616struct mem_block *b = u_mmAllocMem(sim_state.heap, 4096, GMP_ALIGN2, 0);617memset(sim_state.mem + b->ofs - sim_state.mem_base, 0xd0, 4096);618619sim_state.ver = v3d_hw_get_version(sim_state.v3d);620621mtx_unlock(&sim_state.mutex);622623sim_state.fd_map =624_mesa_hash_table_create(NULL,625_mesa_hash_pointer,626_mesa_key_pointer_equal);627628util_dynarray_init(&sim_state.bin_oom, NULL);629630if (sim_state.ver >= 41)631v3d41_simulator_init_regs(sim_state.v3d);632else633v3d33_simulator_init_regs(sim_state.v3d);634}635636struct v3d_simulator_file *637v3d_simulator_init(int fd)638{639v3d_simulator_init_global();640641struct v3d_simulator_file *sim_file = rzalloc(NULL, struct v3d_simulator_file);642643drmVersionPtr version = drmGetVersion(fd);644if (version && strncmp(version->name, "i915", version->name_len) == 0)645sim_file->is_i915 = true;646drmFreeVersion(version);647648sim_file->bo_map =649_mesa_hash_table_create(sim_file,650_mesa_hash_pointer,651_mesa_key_pointer_equal);652653mtx_lock(&sim_state.mutex);654_mesa_hash_table_insert(sim_state.fd_map, int_to_key(fd + 1),655sim_file);656mtx_unlock(&sim_state.mutex);657658sim_file->gmp = u_mmAllocMem(sim_state.heap, 8096, GMP_ALIGN2, 0);659sim_file->gmp_vaddr = (sim_state.mem + sim_file->gmp->ofs -660sim_state.mem_base);661memset(sim_file->gmp_vaddr, 0, 8096);662663return sim_file;664}665666void667v3d_simulator_destroy(struct v3d_simulator_file *sim_file)668{669mtx_lock(&sim_state.mutex);670if (!--sim_state.refcount) {671_mesa_hash_table_destroy(sim_state.fd_map, NULL);672util_dynarray_fini(&sim_state.bin_oom);673u_mmDestroy(sim_state.heap);674/* No memsetting the struct, because it contains the mutex. */675sim_state.mem = NULL;676}677mtx_unlock(&sim_state.mutex);678ralloc_free(sim_file);679}680681#endif /* USE_V3D_SIMULATOR */682683684