Path: blob/21.2-virgl/src/gallium/winsys/svga/drm/vmw_screen_dri.c
4573 views
/**********************************************************1* Copyright 2009-2015 VMware, Inc. All rights reserved.2*3* Permission is hereby granted, free of charge, to any person4* obtaining a copy of this software and associated documentation5* files (the "Software"), to deal in the Software without6* restriction, including without limitation the rights to use, copy,7* modify, merge, publish, distribute, sublicense, and/or sell copies8* of the Software, and to permit persons to whom the Software is9* furnished to do so, subject to the following conditions:10*11* The above copyright notice and this permission notice shall be12* included in all copies or substantial portions of the Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,15* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF16* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND17* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS18* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN19* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN20* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE21* SOFTWARE.22*23**********************************************************/242526#include "pipe/p_compiler.h"27#include "util/u_inlines.h"28#include "util/u_memory.h"29#include "util/format/u_format.h"3031#include "vmw_context.h"32#include "vmw_screen.h"33#include "vmw_surface.h"34#include "vmw_buffer.h"35#include "svga_drm_public.h"36#include "svga3d_surfacedefs.h"3738#include "frontend/drm_driver.h"3940#include "vmwgfx_drm.h"41#include <xf86drm.h>4243#include <stdio.h>44#include <fcntl.h>4546struct dri1_api_version {47int major;48int minor;49int patch_level;50};5152static struct svga_winsys_surface *53vmw_drm_surface_from_handle(struct svga_winsys_screen *sws,54struct winsys_handle *whandle,55SVGA3dSurfaceFormat *format);5657static struct svga_winsys_surface *58vmw_drm_gb_surface_from_handle(struct svga_winsys_screen *sws,59struct winsys_handle *whandle,60SVGA3dSurfaceFormat *format);61static boolean62vmw_drm_surface_get_handle(struct svga_winsys_screen *sws,63struct svga_winsys_surface *surface,64unsigned stride,65struct winsys_handle *whandle);6667static struct dri1_api_version drm_required = { 2, 1, 0 };68static struct dri1_api_version drm_compat = { 2, 0, 0 };6970static boolean71vmw_dri1_check_version(const struct dri1_api_version *cur,72const struct dri1_api_version *required,73const struct dri1_api_version *compat,74const char component[])75{76if (cur->major > required->major && cur->major <= compat->major)77return TRUE;78if (cur->major == required->major && cur->minor >= required->minor)79return TRUE;8081vmw_error("%s version failure.\n", component);82vmw_error("%s version is %d.%d.%d and this driver can only work\n"83"with versions %d.%d.x through %d.x.x.\n",84component,85cur->major, cur->minor, cur->patch_level,86required->major, required->minor, compat->major);87return FALSE;88}8990/* This is actually the entrypoint to the entire driver,91* called by the target bootstrap code.92*/93struct svga_winsys_screen *94svga_drm_winsys_screen_create(int fd)95{96struct vmw_winsys_screen *vws;97struct dri1_api_version drm_ver;98drmVersionPtr ver;99100ver = drmGetVersion(fd);101if (ver == NULL)102return NULL;103104drm_ver.major = ver->version_major;105drm_ver.minor = ver->version_minor;106drm_ver.patch_level = 0; /* ??? */107108drmFreeVersion(ver);109if (!vmw_dri1_check_version(&drm_ver, &drm_required,110&drm_compat, "vmwgfx drm driver"))111return NULL;112113vws = vmw_winsys_create(fd);114if (!vws)115goto out_no_vws;116117/* XXX do this properly */118vws->base.surface_from_handle = vws->base.have_gb_objects ?119vmw_drm_gb_surface_from_handle : vmw_drm_surface_from_handle;120vws->base.surface_get_handle = vmw_drm_surface_get_handle;121122return &vws->base;123124out_no_vws:125return NULL;126}127128/**129* vmw_drm_gb_surface_from_handle - Create a shared surface130*131* @sws: Screen to register the surface with.132* @whandle: struct winsys_handle identifying the kernel surface object133* @format: On successful return points to a value describing the134* surface format.135*136* Returns a refcounted pointer to a struct svga_winsys_surface137* embedded in a struct vmw_svga_winsys_surface on success or NULL138* on failure.139*/140static struct svga_winsys_surface *141vmw_drm_gb_surface_from_handle(struct svga_winsys_screen *sws,142struct winsys_handle *whandle,143SVGA3dSurfaceFormat *format)144{145struct vmw_svga_winsys_surface *vsrf;146struct svga_winsys_surface *ssrf;147struct vmw_winsys_screen *vws = vmw_winsys_screen(sws);148SVGA3dSurfaceAllFlags flags;149uint32_t mip_levels;150struct vmw_buffer_desc desc;151struct pb_manager *provider = vws->pools.gmr;152struct pb_buffer *pb_buf;153uint32_t handle;154int ret;155156if (whandle->offset != 0) {157fprintf(stderr, "Attempt to import unsupported winsys offset %u\n",158whandle->offset);159return NULL;160}161162ret = vmw_ioctl_gb_surface_ref(vws, whandle, &flags, format,163&mip_levels, &handle, &desc.region);164165if (ret) {166fprintf(stderr, "Failed referencing shared surface. SID %d.\n"167"Error %d (%s).\n",168whandle->handle, ret, strerror(-ret));169return NULL;170}171172if (mip_levels != 1) {173fprintf(stderr, "Incorrect number of mipmap levels on shared surface."174" SID %d, levels %d\n",175whandle->handle, mip_levels);176goto out_mip;177}178179vsrf = CALLOC_STRUCT(vmw_svga_winsys_surface);180if (!vsrf)181goto out_mip;182183pipe_reference_init(&vsrf->refcnt, 1);184p_atomic_set(&vsrf->validated, 0);185vsrf->screen = vws;186vsrf->sid = handle;187vsrf->size = vmw_region_size(desc.region);188189/*190* Synchronize backing buffers of shared surfaces using the191* kernel, since we don't pass fence objects around between192* processes.193*/194desc.pb_desc.alignment = 4096;195desc.pb_desc.usage = VMW_BUFFER_USAGE_SHARED | VMW_BUFFER_USAGE_SYNC;196pb_buf = provider->create_buffer(provider, vsrf->size, &desc.pb_desc);197vsrf->buf = vmw_svga_winsys_buffer_wrap(pb_buf);198if (!vsrf->buf)199goto out_no_buf;200ssrf = svga_winsys_surface(vsrf);201202return ssrf;203204out_no_buf:205FREE(vsrf);206out_mip:207vmw_ioctl_region_destroy(desc.region);208vmw_ioctl_surface_destroy(vws, whandle->handle);209return NULL;210}211212static struct svga_winsys_surface *213vmw_drm_surface_from_handle(struct svga_winsys_screen *sws,214struct winsys_handle *whandle,215SVGA3dSurfaceFormat *format)216{217struct vmw_svga_winsys_surface *vsrf;218struct svga_winsys_surface *ssrf;219struct vmw_winsys_screen *vws = vmw_winsys_screen(sws);220union drm_vmw_surface_reference_arg arg;221struct drm_vmw_surface_arg *req = &arg.req;222struct drm_vmw_surface_create_req *rep = &arg.rep;223uint32_t handle = 0;224struct drm_vmw_size size;225SVGA3dSize base_size;226int ret;227int i;228229if (whandle->offset != 0) {230fprintf(stderr, "Attempt to import unsupported winsys offset %u\n",231whandle->offset);232return NULL;233}234235switch (whandle->type) {236case WINSYS_HANDLE_TYPE_SHARED:237case WINSYS_HANDLE_TYPE_KMS:238handle = whandle->handle;239break;240case WINSYS_HANDLE_TYPE_FD:241ret = drmPrimeFDToHandle(vws->ioctl.drm_fd, whandle->handle,242&handle);243if (ret) {244vmw_error("Failed to get handle from prime fd %d.\n",245(int) whandle->handle);246return NULL;247}248break;249default:250vmw_error("Attempt to import unsupported handle type %d.\n",251whandle->type);252return NULL;253}254255memset(&arg, 0, sizeof(arg));256req->sid = handle;257rep->size_addr = (unsigned long)&size;258259ret = drmCommandWriteRead(vws->ioctl.drm_fd, DRM_VMW_REF_SURFACE,260&arg, sizeof(arg));261262/*263* Need to close the handle we got from prime.264*/265if (whandle->type == WINSYS_HANDLE_TYPE_FD)266vmw_ioctl_surface_destroy(vws, handle);267268if (ret) {269/*270* Any attempt to share something other than a surface, like a dumb271* kms buffer, should fail here.272*/273vmw_error("Failed referencing shared surface. SID %d.\n"274"Error %d (%s).\n",275handle, ret, strerror(-ret));276return NULL;277}278279if (rep->mip_levels[0] != 1) {280vmw_error("Incorrect number of mipmap levels on shared surface."281" SID %d, levels %d\n",282handle, rep->mip_levels[0]);283goto out_mip;284}285286for (i=1; i < DRM_VMW_MAX_SURFACE_FACES; ++i) {287if (rep->mip_levels[i] != 0) {288vmw_error("Incorrect number of faces levels on shared surface."289" SID %d, face %d present.\n",290handle, i);291goto out_mip;292}293}294295vsrf = CALLOC_STRUCT(vmw_svga_winsys_surface);296if (!vsrf)297goto out_mip;298299pipe_reference_init(&vsrf->refcnt, 1);300p_atomic_set(&vsrf->validated, 0);301vsrf->screen = vws;302vsrf->sid = handle;303ssrf = svga_winsys_surface(vsrf);304*format = rep->format;305306/* Estimate usage, for early flushing. */307308base_size.width = size.width;309base_size.height = size.height;310base_size.depth = size.depth;311vsrf->size = svga3dsurface_get_serialized_size(rep->format, base_size,312rep->mip_levels[0],313FALSE);314315return ssrf;316317out_mip:318vmw_ioctl_surface_destroy(vws, handle);319320return NULL;321}322323static boolean324vmw_drm_surface_get_handle(struct svga_winsys_screen *sws,325struct svga_winsys_surface *surface,326unsigned stride,327struct winsys_handle *whandle)328{329struct vmw_winsys_screen *vws = vmw_winsys_screen(sws);330struct vmw_svga_winsys_surface *vsrf;331int ret;332333if (!surface)334return FALSE;335336vsrf = vmw_svga_winsys_surface(surface);337whandle->handle = vsrf->sid;338whandle->stride = stride;339whandle->offset = 0;340341switch (whandle->type) {342case WINSYS_HANDLE_TYPE_SHARED:343case WINSYS_HANDLE_TYPE_KMS:344whandle->handle = vsrf->sid;345break;346case WINSYS_HANDLE_TYPE_FD:347ret = drmPrimeHandleToFD(vws->ioctl.drm_fd, vsrf->sid, DRM_CLOEXEC,348(int *)&whandle->handle);349if (ret) {350vmw_error("Failed to get file descriptor from prime.\n");351return FALSE;352}353break;354default:355vmw_error("Attempt to export unsupported handle type %d.\n",356whandle->type);357return FALSE;358}359360return TRUE;361}362363364