Path: blob/21.2-virgl/src/nouveau/drm-shim/nouveau_noop.c
4565 views
/*1* Copyright © 2021 Ilia Mirkin2*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 OTHER20* DEALINGS IN THE SOFTWARE.21*/2223#include <limits.h>24#include <stdio.h>25#include <stdint.h>26#include <stdlib.h>27#include <sys/ioctl.h>28#include <nouveau_drm.h>29#include "drm-shim/drm_shim.h"30#include "util//u_math.h"3132bool drm_shim_driver_prefers_first_render_node = true;3334struct nouveau_device {35uint64_t next_offset;36};3738static struct nouveau_device nouveau = {39.next_offset = 0x1000,40};4142struct nouveau_shim_bo {43struct shim_bo base;44uint64_t offset;45};4647static struct nouveau_shim_bo *48nouveau_shim_bo(struct shim_bo *bo)49{50return (struct nouveau_shim_bo *)bo;51}5253struct nouveau_device_info {54uint32_t chip_id;55};5657static struct nouveau_device_info device_info;5859static int60nouveau_ioctl_noop(int fd, unsigned long request, void *arg)61{62return 0;63}6465static int66nouveau_ioctl_gem_new(int fd, unsigned long request, void *arg)67{68struct shim_fd *shim_fd = drm_shim_fd_lookup(fd);69struct drm_nouveau_gem_new *create = arg;70struct nouveau_shim_bo *bo = calloc(1, sizeof(*bo));7172drm_shim_bo_init(&bo->base, create->info.size);7374assert(ULONG_MAX - nouveau.next_offset > create->info.size);7576create->info.handle = drm_shim_bo_get_handle(shim_fd, &bo->base);77create->info.map_handle = drm_shim_bo_get_mmap_offset(shim_fd, &bo->base);7879if (create->align != 0)80nouveau.next_offset = align64(nouveau.next_offset, create->align);81create->info.offset = nouveau.next_offset;82nouveau.next_offset += create->info.size;8384bo->offset = create->info.offset;8586drm_shim_bo_put(&bo->base);8788return 0;89}9091static int92nouveau_ioctl_gem_info(int fd, unsigned long request, void *arg)93{94struct shim_fd *shim_fd = drm_shim_fd_lookup(fd);95struct drm_nouveau_gem_info *info = arg;96struct nouveau_shim_bo *bo =97nouveau_shim_bo(drm_shim_bo_lookup(shim_fd, info->handle));98info->map_handle = drm_shim_bo_get_mmap_offset(shim_fd, &bo->base);99info->offset = bo->offset;100info->size = bo->base.size;101102drm_shim_bo_put(&bo->base);103104return 0;105}106107static int108nouveau_ioctl_gem_pushbuf(int fd, unsigned long request, void *arg)109{110struct drm_nouveau_gem_pushbuf *submit = arg;111submit->vram_available = 3ULL << 30;112submit->gart_available = 1ULL << 40;113return 0;114}115116static int117nouveau_ioctl_channel_alloc(int fd, unsigned long request, void *arg)118{119struct shim_fd *shim_fd = drm_shim_fd_lookup(fd);120struct drm_nouveau_channel_alloc *alloc = arg;121if (device_info.chip_id == 0x50 || device_info.chip_id >= 0x80)122alloc->pushbuf_domains = NOUVEAU_GEM_DOMAIN_VRAM | NOUVEAU_GEM_DOMAIN_GART;123else124alloc->pushbuf_domains = NOUVEAU_GEM_DOMAIN_GART;125126/* NOTE: this will get leaked since we don't handle the channel127* free. However only one channel is created per screen, so impact should128* be limited. */129struct nouveau_shim_bo *notify = calloc(1, sizeof(*notify));130drm_shim_bo_init(¬ify->base, 0x1000);131notify->offset = nouveau.next_offset;132nouveau.next_offset += 0x1000;133alloc->notifier_handle = drm_shim_bo_get_handle(shim_fd, ¬ify->base);134135drm_shim_bo_put(¬ify->base);136137return 0;138}139140static int141nouveau_ioctl_get_param(int fd, unsigned long request, void *arg)142{143struct drm_nouveau_getparam *gp = arg;144145switch (gp->param) {146case NOUVEAU_GETPARAM_CHIPSET_ID:147gp->value = device_info.chip_id;148return 0;149case NOUVEAU_GETPARAM_PCI_VENDOR:150gp->value = 0x10de;151return 0;152case NOUVEAU_GETPARAM_PCI_DEVICE:153gp->value = 0x1004;154return 0;155case NOUVEAU_GETPARAM_BUS_TYPE:156gp->value = 2 /* NV_PCIE */;157return 0;158case NOUVEAU_GETPARAM_FB_SIZE:159gp->value = 3ULL << 30;160return 0;161case NOUVEAU_GETPARAM_AGP_SIZE:162gp->value = 1ULL << 40;163return 0;164case NOUVEAU_GETPARAM_PTIMER_TIME:165gp->value = 0;166return 0;167case NOUVEAU_GETPARAM_HAS_BO_USAGE:168gp->value = 1;169return 0;170case NOUVEAU_GETPARAM_GRAPH_UNITS:171gp->value = 0x01000001;172return 0;173default:174fprintf(stderr, "Unknown DRM_IOCTL_NOUVEAU_GETPARAM %llu\n",175(long long unsigned)gp->param);176return -1;177}178}179180static ioctl_fn_t driver_ioctls[] = {181[DRM_NOUVEAU_GETPARAM] = nouveau_ioctl_get_param,182[DRM_NOUVEAU_CHANNEL_ALLOC] = nouveau_ioctl_channel_alloc,183[DRM_NOUVEAU_CHANNEL_FREE] = nouveau_ioctl_noop,184[DRM_NOUVEAU_GROBJ_ALLOC] = nouveau_ioctl_noop,185[DRM_NOUVEAU_NOTIFIEROBJ_ALLOC] = nouveau_ioctl_noop,186[DRM_NOUVEAU_GPUOBJ_FREE] = nouveau_ioctl_noop,187[DRM_NOUVEAU_GEM_NEW] = nouveau_ioctl_gem_new,188[DRM_NOUVEAU_GEM_PUSHBUF] = nouveau_ioctl_gem_pushbuf,189[DRM_NOUVEAU_GEM_CPU_PREP] = nouveau_ioctl_noop,190[DRM_NOUVEAU_GEM_INFO] = nouveau_ioctl_gem_info,191};192193static void194nouveau_driver_get_device_info(void)195{196const char *env = getenv("NOUVEAU_CHIPSET");197198if (!env) {199device_info.chip_id = 0xf0;200return;201}202203device_info.chip_id = strtol(env, NULL, 16);204}205206void207drm_shim_driver_init(void)208{209shim_device.bus_type = DRM_BUS_PCI;210shim_device.driver_name = "nouveau";211shim_device.driver_ioctls = driver_ioctls;212shim_device.driver_ioctl_count = ARRAY_SIZE(driver_ioctls);213214shim_device.version_major = 1;215shim_device.version_minor = 0;216shim_device.version_patchlevel = 1;217218nouveau_driver_get_device_info();219220/* nothing looks at the pci id, so fix it to a GTX 780 */221static const char uevent_content[] =222"DRIVER=nouveau\n"223"PCI_CLASS=30000\n"224"PCI_ID=10de:1004\n"225"PCI_SUBSYS_ID=1028:075B\n"226"PCI_SLOT_NAME=0000:01:00.0\n"227"MODALIAS=pci:v000010ded00005916sv00001028sd0000075Bbc03sc00i00\n";228drm_shim_override_file(uevent_content,229"/sys/dev/char/%d:%d/device/uevent",230DRM_MAJOR, render_node_minor);231drm_shim_override_file("0x0\n",232"/sys/dev/char/%d:%d/device/revision",233DRM_MAJOR, render_node_minor);234drm_shim_override_file("0x10de",235"/sys/dev/char/%d:%d/device/vendor",236DRM_MAJOR, render_node_minor);237drm_shim_override_file("0x10de",238"/sys/devices/pci0000:00/0000:01:00.0/vendor");239drm_shim_override_file("0x1004",240"/sys/dev/char/%d:%d/device/device",241DRM_MAJOR, render_node_minor);242drm_shim_override_file("0x1004",243"/sys/devices/pci0000:00/0000:01:00.0/device");244drm_shim_override_file("0x1234",245"/sys/dev/char/%d:%d/device/subsystem_vendor",246DRM_MAJOR, render_node_minor);247drm_shim_override_file("0x1234",248"/sys/devices/pci0000:00/0000:01:00.0/subsystem_vendor");249drm_shim_override_file("0x1234",250"/sys/dev/char/%d:%d/device/subsystem_device",251DRM_MAJOR, render_node_minor);252drm_shim_override_file("0x1234",253"/sys/devices/pci0000:00/0000:01:00.0/subsystem_device");254}255256257