Path: blob/21.2-virgl/src/broadcom/drm-shim/v3dx.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/* @file24*25* v3d driver code interacting v3dv3 simulator/fpga library.26*27* This is compiled per V3D version we support, since the register definitions28* conflict.29*/3031#include <errno.h>32#include <stdbool.h>33#include <stdio.h>34#include <string.h>35#include <sys/mman.h>36#include "util/macros.h"37#include "util/u_mm.h"38#include "broadcom/common/v3d_macros.h"39#include "v3d_simulator_wrapper.h"40#include "drm-shim/drm_shim.h"41#include "drm-uapi/v3d_drm.h"42#include "v3d.h"4344#define HW_REGISTER_RO(x) (x)45#define HW_REGISTER_RW(x) (x)46#if V3D_VERSION >= 4147#include "libs/core/v3d/registers/4.1.34.0/v3d.h"48#else49#include "libs/core/v3d/registers/3.3.0.0/v3d.h"50#endif5152#define V3D_WRITE(reg, val) v3d_hw_write_reg(v3d.hw, reg, val)53#define V3D_READ(reg) v3d_hw_read_reg(v3d.hw, reg)5455static void56v3d_flush_l3()57{58if (!v3d_hw_has_gca(v3d.hw))59return;6061#if V3D_VERSION < 4062uint32_t gca_ctrl = V3D_READ(V3D_GCA_CACHE_CTRL);6364V3D_WRITE(V3D_GCA_CACHE_CTRL, gca_ctrl | V3D_GCA_CACHE_CTRL_FLUSH_SET);65V3D_WRITE(V3D_GCA_CACHE_CTRL, gca_ctrl & ~V3D_GCA_CACHE_CTRL_FLUSH_SET);66#endif67}6869/* Invalidates the L2 cache. This is a read-only cache. */70static void71v3d_flush_l2(void)72{73V3D_WRITE(V3D_CTL_0_L2CACTL,74V3D_CTL_0_L2CACTL_L2CCLR_SET |75V3D_CTL_0_L2CACTL_L2CENA_SET);76}7778/* Invalidates texture L2 cachelines */79static void80v3d_flush_l2t(void)81{82V3D_WRITE(V3D_CTL_0_L2TFLSTA, 0);83V3D_WRITE(V3D_CTL_0_L2TFLEND, ~0);84V3D_WRITE(V3D_CTL_0_L2TCACTL,85V3D_CTL_0_L2TCACTL_L2TFLS_SET |86(0 << V3D_CTL_0_L2TCACTL_L2TFLM_LSB));87}8889/* Invalidates the slice caches. These are read-only caches. */90static void91v3d_flush_slices(void)92{93V3D_WRITE(V3D_CTL_0_SLCACTL, ~0);94}9596static void97v3d_flush_caches(void)98{99v3d_flush_l3();100v3d_flush_l2();101v3d_flush_l2t();102v3d_flush_slices();103}104105static void106v3d_simulator_copy_in_handle(struct shim_fd *shim_fd, int handle)107{108if (!handle)109return;110111struct v3d_bo *bo = v3d_bo_lookup(shim_fd, handle);112113memcpy(bo->sim_vaddr, bo->gem_vaddr, bo->base.size);114}115116static void117v3d_simulator_copy_out_handle(struct shim_fd *shim_fd, int handle)118{119if (!handle)120return;121122struct v3d_bo *bo = v3d_bo_lookup(shim_fd, handle);123124memcpy(bo->gem_vaddr, bo->sim_vaddr, bo->base.size);125}126127static int128v3dX(v3d_ioctl_submit_cl)(int fd, unsigned long request, void *arg)129{130struct shim_fd *shim_fd = drm_shim_fd_lookup(fd);131struct drm_v3d_submit_cl *submit = arg;132uint32_t *bo_handles = (uint32_t *)(uintptr_t)submit->bo_handles;133134for (int i = 0; i < submit->bo_handle_count; i++)135v3d_simulator_copy_in_handle(shim_fd, bo_handles[i]);136137v3d_flush_caches();138139if (submit->qma) {140V3D_WRITE(V3D_CLE_0_CT0QMA, submit->qma);141V3D_WRITE(V3D_CLE_0_CT0QMS, submit->qms);142}143#if V3D_VERSION >= 41144if (submit->qts) {145V3D_WRITE(V3D_CLE_0_CT0QTS,146V3D_CLE_0_CT0QTS_CTQTSEN_SET |147submit->qts);148}149#endif150151fprintf(stderr, "submit %x..%x!\n", submit->bcl_start, submit->bcl_end);152153V3D_WRITE(V3D_CLE_0_CT0QBA, submit->bcl_start);154V3D_WRITE(V3D_CLE_0_CT0QEA, submit->bcl_end);155156/* Wait for bin to complete before firing render, as it seems the157* simulator doesn't implement the semaphores.158*/159while (V3D_READ(V3D_CLE_0_CT0CA) !=160V3D_READ(V3D_CLE_0_CT0EA)) {161v3d_hw_tick(v3d.hw);162}163164fprintf(stderr, "submit %x..%x!\n", submit->rcl_start, submit->rcl_end);165166v3d_flush_caches();167168V3D_WRITE(V3D_CLE_0_CT1QBA, submit->rcl_start);169V3D_WRITE(V3D_CLE_0_CT1QEA, submit->rcl_end);170171while (V3D_READ(V3D_CLE_0_CT1CA) !=172V3D_READ(V3D_CLE_0_CT1EA)) {173v3d_hw_tick(v3d.hw);174}175176for (int i = 0; i < submit->bo_handle_count; i++)177v3d_simulator_copy_out_handle(shim_fd, bo_handles[i]);178179return 0;180}181182static int183v3dX(v3d_ioctl_submit_tfu)(int fd, unsigned long request, void *arg)184{185struct shim_fd *shim_fd = drm_shim_fd_lookup(fd);186struct drm_v3d_submit_tfu *submit = arg;187188v3d_simulator_copy_in_handle(shim_fd, submit->bo_handles[0]);189v3d_simulator_copy_in_handle(shim_fd, submit->bo_handles[1]);190v3d_simulator_copy_in_handle(shim_fd, submit->bo_handles[2]);191v3d_simulator_copy_in_handle(shim_fd, submit->bo_handles[3]);192193int last_vtct = V3D_READ(V3D_TFU_CS) & V3D_TFU_CS_CVTCT_SET;194195V3D_WRITE(V3D_TFU_IIA, submit->iia);196V3D_WRITE(V3D_TFU_IIS, submit->iis);197V3D_WRITE(V3D_TFU_ICA, submit->ica);198V3D_WRITE(V3D_TFU_IUA, submit->iua);199V3D_WRITE(V3D_TFU_IOA, submit->ioa);200V3D_WRITE(V3D_TFU_IOS, submit->ios);201V3D_WRITE(V3D_TFU_COEF0, submit->coef[0]);202V3D_WRITE(V3D_TFU_COEF1, submit->coef[1]);203V3D_WRITE(V3D_TFU_COEF2, submit->coef[2]);204V3D_WRITE(V3D_TFU_COEF3, submit->coef[3]);205206V3D_WRITE(V3D_TFU_ICFG, submit->icfg);207208while ((V3D_READ(V3D_TFU_CS) & V3D_TFU_CS_CVTCT_SET) == last_vtct) {209v3d_hw_tick(v3d.hw);210}211212v3d_simulator_copy_out_handle(shim_fd, submit->bo_handles[0]);213214return 0;215}216217static int218v3dX(v3d_ioctl_create_bo)(int fd, unsigned long request, void *arg)219{220struct shim_fd *shim_fd = drm_shim_fd_lookup(fd);221struct drm_v3d_create_bo *create = arg;222struct v3d_bo *bo = calloc(1, sizeof(*bo));223224drm_shim_bo_init(&bo->base, create->size);225bo->offset = util_vma_heap_alloc(&v3d.heap, create->size, 4096);226if (bo->offset == 0)227return -ENOMEM;228229bo->sim_vaddr = v3d.mem + bo->offset - v3d.mem_base;230#if 0231/* Place a mapping of the BO inside of the simulator's address space232* for V3D memory. This lets us avoid copy in/out for simpenrose, but233* I'm betting we'll need something else for FPGA.234*/235void *sim_addr = v3d.mem + bo->block->ofs;236void *mmap_ret = mmap(sim_addr, create->size, PROT_READ | PROT_WRITE,237MAP_SHARED | MAP_FIXED, bo->base.fd, 0);238assert(mmap_ret == sim_addr);239#else240/* Make a simulator-private mapping of the shim GEM object. */241bo->gem_vaddr = mmap(NULL, bo->base.size,242PROT_READ | PROT_WRITE,243MAP_SHARED,244bo->base.fd, 0);245if (bo->gem_vaddr == MAP_FAILED) {246fprintf(stderr, "v3d: mmap of shim bo failed\n");247abort();248}249#endif250251create->offset = bo->offset;252create->handle = drm_shim_bo_get_handle(shim_fd, &bo->base);253254drm_shim_bo_put(&bo->base);255256return 0;257}258259static int260v3dX(v3d_ioctl_get_param)(int fd, unsigned long request, void *arg)261{262struct drm_v3d_get_param *gp = arg;263static const uint32_t reg_map[] = {264[DRM_V3D_PARAM_V3D_UIFCFG] = V3D_HUB_CTL_UIFCFG,265[DRM_V3D_PARAM_V3D_HUB_IDENT1] = V3D_HUB_CTL_IDENT1,266[DRM_V3D_PARAM_V3D_HUB_IDENT2] = V3D_HUB_CTL_IDENT2,267[DRM_V3D_PARAM_V3D_HUB_IDENT3] = V3D_HUB_CTL_IDENT3,268[DRM_V3D_PARAM_V3D_CORE0_IDENT0] = V3D_CTL_0_IDENT0,269[DRM_V3D_PARAM_V3D_CORE0_IDENT1] = V3D_CTL_0_IDENT1,270[DRM_V3D_PARAM_V3D_CORE0_IDENT2] = V3D_CTL_0_IDENT2,271};272273switch (gp->param) {274case DRM_V3D_PARAM_SUPPORTS_TFU:275gp->value = 1;276return 0;277}278279if (gp->param < ARRAY_SIZE(reg_map) && reg_map[gp->param]) {280gp->value = V3D_READ(reg_map[gp->param]);281return 0;282}283284fprintf(stderr, "Unknown DRM_IOCTL_V3D_GET_PARAM %d\n", gp->param);285return -1;286}287288static ioctl_fn_t driver_ioctls[] = {289[DRM_V3D_SUBMIT_CL] = v3dX(v3d_ioctl_submit_cl),290[DRM_V3D_SUBMIT_TFU] = v3dX(v3d_ioctl_submit_tfu),291[DRM_V3D_WAIT_BO] = v3d_ioctl_wait_bo,292[DRM_V3D_CREATE_BO] = v3dX(v3d_ioctl_create_bo),293[DRM_V3D_GET_PARAM] = v3dX(v3d_ioctl_get_param),294[DRM_V3D_MMAP_BO] = v3d_ioctl_mmap_bo,295[DRM_V3D_GET_BO_OFFSET] = v3d_ioctl_get_bo_offset,296};297298static void299v3d_isr(uint32_t hub_status)300{301/* Check the per-core bits */302if (hub_status & (1 << 0)) {303uint32_t core_status = V3D_READ(V3D_CTL_0_INT_STS);304305if (core_status & V3D_CTL_0_INT_STS_INT_GMPV_SET) {306fprintf(stderr, "GMP violation at 0x%08x\n",307V3D_READ(V3D_GMP_0_VIO_ADDR));308abort();309} else {310fprintf(stderr,311"Unexpected ISR with core status 0x%08x\n",312core_status);313}314abort();315}316317return;318}319320static void321v3dX(simulator_init_regs)(void)322{323#if V3D_VERSION == 33324/* Set OVRTMUOUT to match kernel behavior.325*326* This means that the texture sampler uniform configuration's tmu327* output type field is used, instead of using the hardware default328* behavior based on the texture type. If you want the default329* behavior, you can still put "2" in the indirect texture state's330* output_type field.331*/332V3D_WRITE(V3D_CTL_0_MISCCFG, V3D_CTL_1_MISCCFG_OVRTMUOUT_SET);333#endif334335uint32_t core_interrupts = V3D_CTL_0_INT_STS_INT_GMPV_SET;336V3D_WRITE(V3D_CTL_0_INT_MSK_SET, ~core_interrupts);337V3D_WRITE(V3D_CTL_0_INT_MSK_CLR, core_interrupts);338339v3d_hw_set_isr(v3d.hw, v3d_isr);340}341342static void343v3d_bo_free(struct shim_bo *shim_bo)344{345struct v3d_bo *bo = v3d_bo(shim_bo);346347if (bo->gem_vaddr)348munmap(bo->gem_vaddr, shim_bo->size);349350util_vma_heap_free(&v3d.heap, bo->offset, bo->base.size);351}352353void354v3dX(drm_shim_driver_init)(void)355{356shim_device.driver_ioctls = driver_ioctls;357shim_device.driver_ioctl_count = ARRAY_SIZE(driver_ioctls);358359shim_device.driver_bo_free = v3d_bo_free;360361/* Allocate a gig of memory to play in. */362v3d_hw_alloc_mem(v3d.hw, 1024 * 1024 * 1024);363v3d.mem_base =364v3d_hw_get_mem(v3d.hw, &v3d.mem_size,365&v3d.mem);366util_vma_heap_init(&v3d.heap, 4096, v3d.mem_size - 4096);367368v3dX(simulator_init_regs)();369}370371372