diff options
author | Oscar Molin | 2016-06-09 20:19:11 +0200 |
---|---|---|
committer | Oscar Molin | 2016-06-09 20:19:11 +0200 |
commit | c0e3a24c3c377b677124f61de25cd657bf73ce17 (patch) | |
tree | 8f9bbab6badd12b271f3d82d60dce87860903db4 | |
parent | 2b14b643ed5807202518a9156909e67972c24c2a (diff) | |
download | aur-c0e3a24c3c377b677124f61de25cd657bf73ce17.tar.gz |
5.0.20-2
-rw-r--r-- | VBoxNetAdp-linux.c | 440 | ||||
-rw-r--r-- | vbox_drv.h | 264 | ||||
-rw-r--r-- | vbox_main.c | 499 | ||||
-rw-r--r-- | vbox_mode.c | 720 |
4 files changed, 1923 insertions, 0 deletions
diff --git a/VBoxNetAdp-linux.c b/VBoxNetAdp-linux.c new file mode 100644 index 000000000000..57ea4dde6e4c --- /dev/null +++ b/VBoxNetAdp-linux.c @@ -0,0 +1,440 @@ +/* $Id$ */ +/** @file + * VBoxNetAdp - Virtual Network Adapter Driver (Host), Linux Specific Code. + */ +/* + * Copyright (C) 2009-2015 Oracle Corporation + * + * This file is part of VirtualBox Open Source Edition (OSE), as + * available from http://www.virtualbox.org. This file is free software; + * you can redistribute it and/or modify it under the terms of the GNU + * General Public License (GPL) as published by the Free Software + * Foundation, in version 2 as it comes in the "COPYING" file of the + * VirtualBox OSE distribution. VirtualBox OSE is distributed in the + * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind. + */ +/********************************************************************************************************************************* +* Header Files * +*********************************************************************************************************************************/ +#include "the-linux-kernel.h" +#include "version-generated.h" +#include "revision-generated.h" +#include "product-generated.h" +#include <linux/ethtool.h> +#include <linux/netdevice.h> +#include <linux/etherdevice.h> +#include <linux/miscdevice.h> +#define LOG_GROUP LOG_GROUP_NET_ADP_DRV +#include <VBox/log.h> +#include <VBox/err.h> +#include <iprt/process.h> +#include <iprt/initterm.h> +#include <iprt/mem.h> +#include <iprt/string.h> +/* +#include <iprt/assert.h> +#include <iprt/semaphore.h> +#include <iprt/spinlock.h> +#include <iprt/string.h> +#include <iprt/uuid.h> +#include <iprt/alloca.h> +*/ +#define VBOXNETADP_OS_SPECFIC 1 +#include "../VBoxNetAdpInternal.h" +/********************************************************************************************************************************* +* Defined Constants And Macros * +*********************************************************************************************************************************/ +#define VBOXNETADP_LINUX_NAME "vboxnet%d" +#define VBOXNETADP_CTL_DEV_NAME "vboxnetctl" +#define VBOXNETADP_FROM_IFACE(iface) ((PVBOXNETADP) ifnet_softc(iface)) +/********************************************************************************************************************************* +* Internal Functions * +*********************************************************************************************************************************/ +static int VBoxNetAdpLinuxInit(void); +static void VBoxNetAdpLinuxUnload(void); +static int VBoxNetAdpLinuxOpen(struct inode *pInode, struct file *pFilp); +static int VBoxNetAdpLinuxClose(struct inode *pInode, struct file *pFilp); +#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 36) +static int VBoxNetAdpLinuxIOCtl(struct inode *pInode, struct file *pFilp, + unsigned int uCmd, unsigned long ulArg); +#else /* LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 36) */ +static long VBoxNetAdpLinuxIOCtlUnlocked(struct file *pFilp, + unsigned int uCmd, unsigned long ulArg); +#endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 36) */ +static void vboxNetAdpEthGetDrvinfo(struct net_device *dev, struct ethtool_drvinfo *info); +static int vboxNetAdpEthGetSettings(struct net_device *dev, struct ethtool_cmd *cmd); +/********************************************************************************************************************************* +* Global Variables * +*********************************************************************************************************************************/ +module_init(VBoxNetAdpLinuxInit); +module_exit(VBoxNetAdpLinuxUnload); +MODULE_AUTHOR(VBOX_VENDOR); +MODULE_DESCRIPTION(VBOX_PRODUCT " Network Adapter Driver"); +MODULE_LICENSE("GPL"); +#ifdef MODULE_VERSION +MODULE_VERSION(VBOX_VERSION_STRING " r" RT_XSTR(VBOX_SVN_REV) " (" RT_XSTR(INTNETTRUNKIFPORT_VERSION) ")"); +#endif +/** + * The (common) global data. + */ +static struct file_operations gFileOpsVBoxNetAdp = +{ + owner: THIS_MODULE, + open: VBoxNetAdpLinuxOpen, + release: VBoxNetAdpLinuxClose, +#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 36) + ioctl: VBoxNetAdpLinuxIOCtl, +#else /* LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 36) */ + unlocked_ioctl: VBoxNetAdpLinuxIOCtlUnlocked, +#endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 36) */ +}; +/** The miscdevice structure. */ +static struct miscdevice g_CtlDev = +{ + minor: MISC_DYNAMIC_MINOR, + name: VBOXNETADP_CTL_DEV_NAME, + fops: &gFileOpsVBoxNetAdp, +# if LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 17) + devfs_name: VBOXNETADP_CTL_DEV_NAME +# endif +}; +static const struct ethtool_ops gEthToolOpsVBoxNetAdp = +{ + .get_drvinfo = vboxNetAdpEthGetDrvinfo, + .get_settings = vboxNetAdpEthGetSettings, + .get_link = ethtool_op_get_link, +}; +struct VBoxNetAdpPriv +{ + struct net_device_stats Stats; +}; +typedef struct VBoxNetAdpPriv VBOXNETADPPRIV; +typedef VBOXNETADPPRIV *PVBOXNETADPPRIV; +static int vboxNetAdpLinuxOpen(struct net_device *pNetDev) +{ + netif_start_queue(pNetDev); + return 0; +} +static int vboxNetAdpLinuxStop(struct net_device *pNetDev) +{ + netif_stop_queue(pNetDev); + return 0; +} +static int vboxNetAdpLinuxXmit(struct sk_buff *pSkb, struct net_device *pNetDev) +{ + PVBOXNETADPPRIV pPriv = netdev_priv(pNetDev); + /* Update the stats. */ + pPriv->Stats.tx_packets++; + pPriv->Stats.tx_bytes += pSkb->len; +#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 31) + /* Update transmission time stamp. */ + pNetDev->trans_start = jiffies; +#endif + /* Nothing else to do, just free the sk_buff. */ + dev_kfree_skb(pSkb); + return 0; +} +struct net_device_stats *vboxNetAdpLinuxGetStats(struct net_device *pNetDev) +{ + PVBOXNETADPPRIV pPriv = netdev_priv(pNetDev); + return &pPriv->Stats; +} +/* ethtool_ops::get_drvinfo */ +static void vboxNetAdpEthGetDrvinfo(struct net_device *pNetDev, struct ethtool_drvinfo *info) +{ + PVBOXNETADPPRIV pPriv = netdev_priv(pNetDev); + NOREF(pPriv); + RTStrPrintf(info->driver, sizeof(info->driver), + "%s", VBOXNETADP_NAME); + /* + * Would be nice to include VBOX_SVN_REV, but it's not available + * here. Use file's svn revision via svn keyword? + */ + RTStrPrintf(info->version, sizeof(info->version), + "%s", VBOX_VERSION_STRING); + RTStrPrintf(info->fw_version, sizeof(info->fw_version), + "0x%08X", INTNETTRUNKIFPORT_VERSION); + RTStrPrintf(info->bus_info, sizeof(info->driver), + "N/A"); +} +/* ethtool_ops::get_settings */ +static int vboxNetAdpEthGetSettings(struct net_device *pNetDev, struct ethtool_cmd *cmd) +{ + cmd->supported = 0; + cmd->advertising = 0; +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 27) + ethtool_cmd_speed_set(cmd, SPEED_10); +#else + cmd->speed = SPEED_10; +#endif + cmd->duplex = DUPLEX_FULL; + cmd->port = PORT_TP; + cmd->phy_address = 0; + cmd->transceiver = XCVR_INTERNAL; + cmd->autoneg = AUTONEG_DISABLE; + cmd->maxtxpkt = 0; + cmd->maxrxpkt = 0; + return 0; +} +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 29) +static const struct net_device_ops vboxNetAdpNetdevOps = { + .ndo_open = vboxNetAdpLinuxOpen, + .ndo_stop = vboxNetAdpLinuxStop, + .ndo_start_xmit = vboxNetAdpLinuxXmit, + .ndo_get_stats = vboxNetAdpLinuxGetStats +}; +#endif +static void vboxNetAdpNetDevInit(struct net_device *pNetDev) +{ + PVBOXNETADPPRIV pPriv; + ether_setup(pNetDev); +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 29) + pNetDev->netdev_ops = &vboxNetAdpNetdevOps; +#else /* LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 29) */ + pNetDev->open = vboxNetAdpLinuxOpen; + pNetDev->stop = vboxNetAdpLinuxStop; + pNetDev->hard_start_xmit = vboxNetAdpLinuxXmit; + pNetDev->get_stats = vboxNetAdpLinuxGetStats; +#endif /* LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 29) */ + pNetDev->ethtool_ops = &gEthToolOpsVBoxNetAdp; + pPriv = netdev_priv(pNetDev); + memset(pPriv, 0, sizeof(*pPriv)); +} +int vboxNetAdpOsCreate(PVBOXNETADP pThis, PCRTMAC pMACAddress) +{ + int rc = VINF_SUCCESS; + struct net_device *pNetDev; + /* No need for private data. */ + pNetDev = alloc_netdev(sizeof(VBOXNETADPPRIV), + pThis->szName[0] ? pThis->szName : VBOXNETADP_LINUX_NAME, +#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 17, 0) + NET_NAME_UNKNOWN, +#endif + vboxNetAdpNetDevInit); + if (pNetDev) + { + int err; + if (pNetDev->dev_addr) + { + memcpy(pNetDev->dev_addr, pMACAddress, ETH_ALEN); + Log2(("vboxNetAdpOsCreate: pNetDev->dev_addr = %.6Rhxd\n", pNetDev->dev_addr)); + /* + * We treat presence of VBoxNetFlt filter as our "carrier", + * see vboxNetFltSetLinkState(). + * + * operstates.txt: "On device allocation, networking core + * sets the flags equivalent to netif_carrier_ok() and + * !netif_dormant()" - so turn carrier off here. + */ + netif_carrier_off(pNetDev); + err = register_netdev(pNetDev); + if (!err) + { + strncpy(pThis->szName, pNetDev->name, sizeof(pThis->szName)); + pThis->szName[sizeof(pThis->szName) - 1] = '\0'; + pThis->u.s.pNetDev = pNetDev; + Log2(("vboxNetAdpOsCreate: pThis=%p pThis->szName = %p\n", pThis, pThis->szName)); + return VINF_SUCCESS; + } + } + else + { + LogRel(("VBoxNetAdp: failed to set MAC address (dev->dev_addr == NULL)\n")); + err = EFAULT; + } + free_netdev(pNetDev); + rc = RTErrConvertFromErrno(err); + } + return rc; +} +void vboxNetAdpOsDestroy(PVBOXNETADP pThis) +{ + struct net_device *pNetDev = pThis->u.s.pNetDev; + AssertPtr(pThis->u.s.pNetDev); + pThis->u.s.pNetDev = NULL; + unregister_netdev(pNetDev); + free_netdev(pNetDev); +} +/** + * Device open. Called on open /dev/vboxnetctl + * + * @param pInode Pointer to inode info structure. + * @param pFilp Associated file pointer. + */ +static int VBoxNetAdpLinuxOpen(struct inode *pInode, struct file *pFilp) +{ + Log(("VBoxNetAdpLinuxOpen: pid=%d/%d %s\n", RTProcSelf(), current->pid, current->comm)); +#ifdef VBOX_WITH_HARDENING + /* + * Only root is allowed to access the device, enforce it! + */ + if (!capable(CAP_SYS_ADMIN)) + { + Log(("VBoxNetAdpLinuxOpen: admin privileges required!\n")); + return -EPERM; + } +#endif + return 0; +} +/** + * Close device. + * + * @param pInode Pointer to inode info structure. + * @param pFilp Associated file pointer. + */ +static int VBoxNetAdpLinuxClose(struct inode *pInode, struct file *pFilp) +{ + Log(("VBoxNetAdpLinuxClose: pid=%d/%d %s\n", + RTProcSelf(), current->pid, current->comm)); + pFilp->private_data = NULL; + return 0; +} +/** + * Device I/O Control entry point. + * + * @param pFilp Associated file pointer. + * @param uCmd The function specified to ioctl(). + * @param ulArg The argument specified to ioctl(). + */ +#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 36) +static int VBoxNetAdpLinuxIOCtl(struct inode *pInode, struct file *pFilp, + unsigned int uCmd, unsigned long ulArg) +#else /* LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 36) */ +static long VBoxNetAdpLinuxIOCtlUnlocked(struct file *pFilp, + unsigned int uCmd, unsigned long ulArg) +#endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 36) */ +{ + VBOXNETADPREQ Req; + PVBOXNETADP pAdp; + int rc; + char *pszName = NULL; + Log(("VBoxNetAdpLinuxIOCtl: param len %#x; uCmd=%#x; add=%#x\n", _IOC_SIZE(uCmd), uCmd, VBOXNETADP_CTL_ADD)); + if (RT_UNLIKELY(_IOC_SIZE(uCmd) != sizeof(Req))) /* paranoia */ + { + Log(("VBoxNetAdpLinuxIOCtl: bad ioctl sizeof(Req)=%#x _IOC_SIZE=%#x; uCmd=%#x.\n", sizeof(Req), _IOC_SIZE(uCmd), uCmd)); + return -EINVAL; + } + switch (uCmd) + { + case VBOXNETADP_CTL_ADD: + Log(("VBoxNetAdpLinuxIOCtl: _IOC_DIR(uCmd)=%#x; IOC_OUT=%#x\n", _IOC_DIR(uCmd), IOC_OUT)); + if (RT_UNLIKELY(copy_from_user(&Req, (void *)ulArg, sizeof(Req)))) + { + Log(("VBoxNetAdpLinuxIOCtl: copy_from_user(,%#lx,) failed; uCmd=%#x.\n", ulArg, uCmd)); + return -EFAULT; + } + Log(("VBoxNetAdpLinuxIOCtl: Add %s\n", Req.szName)); + if (Req.szName[0]) + { + pAdp = vboxNetAdpFindByName(Req.szName); + if (pAdp) + { + Log(("VBoxNetAdpLinuxIOCtl: '%s' already exists\n", Req.szName)); + return -EINVAL; + } + pszName = Req.szName; + } + rc = vboxNetAdpCreate(&pAdp, pszName); + if (RT_FAILURE(rc)) + { + Log(("VBoxNetAdpLinuxIOCtl: vboxNetAdpCreate -> %Rrc\n", rc)); + return -(rc == VERR_OUT_OF_RESOURCES ? ENOMEM : EINVAL); + } + Assert(strlen(pAdp->szName) < sizeof(Req.szName)); + strncpy(Req.szName, pAdp->szName, sizeof(Req.szName) - 1); + Req.szName[sizeof(Req.szName) - 1] = '\0'; + if (RT_UNLIKELY(copy_to_user((void *)ulArg, &Req, sizeof(Req)))) + { + /* this is really bad! */ + /** @todo remove the adapter again? */ + printk(KERN_ERR "VBoxNetAdpLinuxIOCtl: copy_to_user(%#lx,,%#zx); uCmd=%#x!\n", ulArg, sizeof(Req), uCmd); + return -EFAULT; + } + Log(("VBoxNetAdpLinuxIOCtl: Successfully added '%s'\n", Req.szName)); + break; + case VBOXNETADP_CTL_REMOVE: + if (RT_UNLIKELY(copy_from_user(&Req, (void *)ulArg, sizeof(Req)))) + { + Log(("VBoxNetAdpLinuxIOCtl: copy_from_user(,%#lx,) failed; uCmd=%#x.\n", ulArg, uCmd)); + return -EFAULT; + } + Log(("VBoxNetAdpLinuxIOCtl: Remove %s\n", Req.szName)); + pAdp = vboxNetAdpFindByName(Req.szName); + if (!pAdp) + { + Log(("VBoxNetAdpLinuxIOCtl: '%s' not found\n", Req.szName)); + return -EINVAL; + } + rc = vboxNetAdpDestroy(pAdp); + if (RT_FAILURE(rc)) + { + Log(("VBoxNetAdpLinuxIOCtl: vboxNetAdpDestroy('%s') -> %Rrc\n", Req.szName, rc)); + return -EINVAL; + } + Log(("VBoxNetAdpLinuxIOCtl: Successfully removed '%s'\n", Req.szName)); + break; + default: + printk(KERN_ERR "VBoxNetAdpLinuxIOCtl: unknown command %x.\n", uCmd); + return -EINVAL; + } + return 0; +} +int vboxNetAdpOsInit(PVBOXNETADP pThis) +{ + /* + * Init linux-specific members. + */ + pThis->u.s.pNetDev = NULL; + return VINF_SUCCESS; +} +/** + * Initialize module. + * + * @returns appropriate status code. + */ +static int __init VBoxNetAdpLinuxInit(void) +{ + int rc; + /* + * Initialize IPRT. + */ + rc = RTR0Init(0); + if (RT_SUCCESS(rc)) + { + Log(("VBoxNetAdpLinuxInit\n")); + rc = vboxNetAdpInit(); + if (RT_SUCCESS(rc)) + { + rc = misc_register(&g_CtlDev); + if (rc) + { + printk(KERN_ERR "VBoxNetAdp: Can't register " VBOXNETADP_CTL_DEV_NAME " device! rc=%d\n", rc); + return rc; + } + LogRel(("VBoxNetAdp: Successfully started.\n")); + return 0; + } + else + LogRel(("VBoxNetAdp: failed to register vboxnet0 device (rc=%d)\n", rc)); + } + else + LogRel(("VBoxNetAdp: failed to initialize IPRT (rc=%d)\n", rc)); + return -RTErrConvertToErrno(rc); +} +/** + * Unload the module. + * + * @todo We have to prevent this if we're busy! + */ +static void __exit VBoxNetAdpLinuxUnload(void) +{ + Log(("VBoxNetAdpLinuxUnload\n")); + /* + * Undo the work done during start (in reverse order). + */ + vboxNetAdpShutdown(); + /* Remove control device */ + misc_deregister(&g_CtlDev); + RTR0Term(); + Log(("VBoxNetAdpLinuxUnload - done\n")); +}
\ No newline at end of file diff --git a/vbox_drv.h b/vbox_drv.h new file mode 100644 index 000000000000..a2feca85a2a8 --- /dev/null +++ b/vbox_drv.h @@ -0,0 +1,264 @@ +/* $Id$ */ +/** @file + * VirtualBox Additions Linux kernel video driver + */ +/* + * Copyright (C) 2013 Oracle Corporation + * + * This file is part of VirtualBox Open Source Edition (OSE), as + * available from http://www.virtualbox.org. This file is free software; + * you can redistribute it and/or modify it under the terms of the GNU + * General Public License (GPL) as published by the Free Software + * Foundation, in version 2 as it comes in the "COPYING" file of the + * VirtualBox OSE distribution. VirtualBox OSE is distributed in the + * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind. + * -------------------------------------------------------------------- + * + * This code is based on + * ast_drv.h + * with the following copyright and permission notice: + * + * Copyright 2012 Red Hat Inc. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sub license, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL + * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR + * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE + * USE OR OTHER DEALINGS IN THE SOFTWARE. + * + * The above copyright notice and this permission notice (including the + * next paragraph) shall be included in all copies or substantial portions + * of the Software. + * + */ +/* + * Authors: Dave Airlie <airlied@redhat.com> + */ +#ifndef __VBOX_DRV_H__ +#define __VBOX_DRV_H__ +#define LOG_GROUP LOG_GROUP_DEV_VGA +#include "the-linux-kernel.h" +#include <VBox/VBoxVideoGuest.h> +#include <VBox/log.h> +#include <drm/drmP.h> +#include <drm/drm_fb_helper.h> +#include <drm/ttm/ttm_bo_api.h> +#include <drm/ttm/ttm_bo_driver.h> +#include <drm/ttm/ttm_placement.h> +#include <drm/ttm/ttm_memory.h> +#include <drm/ttm/ttm_module.h> +#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 18, 0) +# include <drm/drm_gem.h> +#endif +/* #include "vboxvideo.h" */ +#include "product-generated.h" +#define DRIVER_AUTHOR VBOX_VENDOR +#define DRIVER_NAME "vboxvideo" +#define DRIVER_DESC VBOX_PRODUCT " Graphics Card" +#define DRIVER_DATE "20130823" +#define DRIVER_MAJOR 1 +#define DRIVER_MINOR 0 +#define DRIVER_PATCHLEVEL 0 +#define VBOX_MAX_CURSOR_WIDTH 64 +#define VBOX_MAX_CURSOR_HEIGHT 64 +#define CURSOR_PIXEL_COUNT VBOX_MAX_CURSOR_WIDTH * VBOX_MAX_CURSOR_HEIGHT +#define CURSOR_DATA_SIZE CURSOR_PIXEL_COUNT * 4 + CURSOR_PIXEL_COUNT / 8 +struct vbox_fbdev; +struct vbox_private { + struct drm_device *dev; + uint8_t __iomem *vram; + HGSMIGUESTCOMMANDCONTEXT submit_info; + struct VBVABUFFERCONTEXT *vbva_info; + bool any_pitch; + unsigned num_crtcs; + bool vga2_clone; + /** Amount of available VRAM, including space used for buffers. */ + uint32_t full_vram_size; + /** Amount of available VRAM, not including space used for buffers. */ + uint32_t vram_size; + /** Offset to the host flags in the VRAM. */ + uint32_t host_flags_offset; + /** Array of structures for receiving mode hints. */ + VBVAMODEHINT *last_mode_hints; + struct vbox_fbdev *fbdev; + int fb_mtrr; + struct { + struct drm_global_reference mem_global_ref; + struct ttm_bo_global_ref bo_global_ref; + struct ttm_bo_device bdev; + bool mm_initialised; + } ttm; + struct mutex hw_mutex; + bool isr_installed; + /** We decide whether or not user-space supports display hot-plug + * depending on whether they react to a hot-plug event after the initial + * mode query. */ + bool initial_mode_queried; + struct work_struct hotplug_work; + uint32_t input_mapping_width; + uint32_t input_mapping_height; + uint32_t cursor_width; + uint32_t cursor_height; + uint32_t cursor_hot_x; + uint32_t cursor_hot_y; + size_t cursor_data_size; + uint8_t cursor_data[CURSOR_DATA_SIZE]; +}; +#undef CURSOR_PIXEL_COUNT +#undef CURSOR_DATA_SIZE +int vbox_driver_load(struct drm_device *dev, unsigned long flags); +int vbox_driver_unload(struct drm_device *dev); +void vbox_driver_lastclose(struct drm_device *dev); +struct vbox_gem_object; +#ifndef VGA_PORT_HGSMI_HOST +# define VGA_PORT_HGSMI_HOST 0x3b0 +# define VGA_PORT_HGSMI_GUEST 0x3d0 +#endif +struct vbox_connector { + struct drm_connector base; + char name[32]; + struct vbox_crtc *vbox_crtc; + struct { + uint16_t width; + uint16_t height; + bool disconnected; + } mode_hint; +}; +struct vbox_crtc { + struct drm_crtc base; + bool blanked; + bool disconnected; + unsigned crtc_id; + uint32_t fb_offset; + bool cursor_enabled; +}; +struct vbox_encoder { + struct drm_encoder base; +}; +struct vbox_framebuffer { + struct drm_framebuffer base; + struct drm_gem_object *obj; +}; +struct vbox_fbdev { + struct drm_fb_helper helper; + struct vbox_framebuffer afb; + void *sysram; + int size; + struct ttm_bo_kmap_obj mapping; + int x1, y1, x2, y2; /* dirty rect */ + spinlock_t dirty_lock; +}; +#define to_vbox_crtc(x) container_of(x, struct vbox_crtc, base) +#define to_vbox_connector(x) container_of(x, struct vbox_connector, base) +#define to_vbox_encoder(x) container_of(x, struct vbox_encoder, base) +#define to_vbox_framebuffer(x) container_of(x, struct vbox_framebuffer, base) +extern int vbox_mode_init(struct drm_device *dev); +extern void vbox_mode_fini(struct drm_device *dev); +#if LINUX_VERSION_CODE < KERNEL_VERSION(3, 3, 0) +# define DRM_MODE_FB_CMD drm_mode_fb_cmd +#else +# define DRM_MODE_FB_CMD drm_mode_fb_cmd2 +#endif +#if LINUX_VERSION_CODE < KERNEL_VERSION(3, 15, 0) +# define CRTC_FB(crtc) (crtc)->fb +#else +# define CRTC_FB(crtc) (crtc)->primary->fb +#endif +void vbox_enable_accel(struct vbox_private *vbox); +void vbox_disable_accel(struct vbox_private *vbox); +void vbox_enable_caps(struct vbox_private *vbox); +void vbox_framebuffer_dirty_rectangles(struct drm_framebuffer *fb, + struct drm_clip_rect *rects, + unsigned num_rects); +int vbox_framebuffer_init(struct drm_device *dev, + struct vbox_framebuffer *vbox_fb, +#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 5, 0) + const +#endif + struct DRM_MODE_FB_CMD *mode_cmd, + struct drm_gem_object *obj); +int vbox_fbdev_init(struct drm_device *dev); +void vbox_fbdev_fini(struct drm_device *dev); +void vbox_fbdev_set_suspend(struct drm_device *dev, int state); +void vbox_fbdev_set_base(struct vbox_private *vbox, unsigned long gpu_addr); +struct vbox_bo { + struct ttm_buffer_object bo; + struct ttm_placement placement; + struct ttm_bo_kmap_obj kmap; + struct drm_gem_object gem; +#if LINUX_VERSION_CODE < KERNEL_VERSION(3, 18, 0) + u32 placements[3]; +#else + struct ttm_place placements[3]; +#endif + int pin_count; +}; +#define gem_to_vbox_bo(gobj) container_of((gobj), struct vbox_bo, gem) +static inline struct vbox_bo * +vbox_bo(struct ttm_buffer_object *bo) +{ + return container_of(bo, struct vbox_bo, bo); +} +#define to_vbox_obj(x) container_of(x, struct vbox_gem_object, base) +extern int vbox_dumb_create(struct drm_file *file, + struct drm_device *dev, + struct drm_mode_create_dumb *args); +#if LINUX_VERSION_CODE < KERNEL_VERSION(3, 12, 0) +extern int vbox_dumb_destroy(struct drm_file *file, + struct drm_device *dev, + uint32_t handle); +#endif +extern void vbox_gem_free_object(struct drm_gem_object *obj); +extern int vbox_dumb_mmap_offset(struct drm_file *file, + struct drm_device *dev, + uint32_t handle, + uint64_t *offset); +#define DRM_FILE_PAGE_OFFSET (0x10000000ULL >> PAGE_SHIFT) +int vbox_mm_init(struct vbox_private *vbox); +void vbox_mm_fini(struct vbox_private *vbox); +int vbox_bo_create(struct drm_device *dev, int size, int align, + uint32_t flags, struct vbox_bo **pvboxbo); +int vbox_gem_create(struct drm_device *dev, + u32 size, bool iskernel, + struct drm_gem_object **obj); +int vbox_bo_pin(struct vbox_bo *bo, u32 pl_flag, u64 *gpu_addr); +int vbox_bo_unpin(struct vbox_bo *bo); +static inline int vbox_bo_reserve(struct vbox_bo *bo, bool no_wait) +{ + int ret; +#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 7, 0) + ret = ttm_bo_reserve(&bo->bo, true, no_wait, NULL); +#else + ret = ttm_bo_reserve(&bo->bo, true, no_wait, false, 0); +#endif + if (ret) + { + if (ret != -ERESTARTSYS && ret != -EBUSY) + DRM_ERROR("reserve failed %p\n", bo); + return ret; + } + return 0; +} +static inline void vbox_bo_unreserve(struct vbox_bo *bo) +{ + ttm_bo_unreserve(&bo->bo); +} +void vbox_ttm_placement(struct vbox_bo *bo, int domain); +int vbox_bo_push_sysram(struct vbox_bo *bo); +int vbox_mmap(struct file *filp, struct vm_area_struct *vma); +/* vbox_irq.c */ +int vbox_irq_init(struct vbox_private *vbox); +void vbox_irq_fini(struct vbox_private *vbox); +void vbox_report_hotplug(struct vbox_private *vbox); +irqreturn_t vbox_irq_handler(int irq, void *arg); +#endif
\ No newline at end of file diff --git a/vbox_main.c b/vbox_main.c new file mode 100644 index 000000000000..1cd1b58c99e8 --- /dev/null +++ b/vbox_main.c @@ -0,0 +1,499 @@ +/* $Id$ */ +/** @file + * VirtualBox Additions Linux kernel video driver + */ +/* + * Copyright (C) 2013 Oracle Corporation + * + * This file is part of VirtualBox Open Source Edition (OSE), as + * available from http://www.virtualbox.org. This file is free software; + * you can redistribute it and/or modify it under the terms of the GNU + * General Public License (GPL) as published by the Free Software + * Foundation, in version 2 as it comes in the "COPYING" file of the + * VirtualBox OSE distribution. VirtualBox OSE is distributed in the + * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind. + * -------------------------------------------------------------------- + * + * This code is based on + * ast_main.c + * with the following copyright and permission notice: + * + * Copyright 2012 Red Hat Inc. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sub license, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL + * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR + * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE + * USE OR OTHER DEALINGS IN THE SOFTWARE. + * + * The above copyright notice and this permission notice (including the + * next paragraph) shall be included in all copies or substantial portions + * of the Software. + * + */ +/* + * Authors: Dave Airlie <airlied@redhat.com> + */ +#include "vbox_drv.h" +#include <VBox/VBoxVideoGuest.h> +#include <VBox/VBoxVideo.h> +#include <drm/drm_fb_helper.h> +#include <drm/drm_crtc_helper.h> +static void vbox_user_framebuffer_destroy(struct drm_framebuffer *fb) +{ + struct vbox_framebuffer *vbox_fb = to_vbox_framebuffer(fb); + if (vbox_fb->obj) + drm_gem_object_unreference_unlocked(vbox_fb->obj); + LogFunc(("vboxvideo: %d: vbox_fb=%p, vbox_fb->obj=%p\n", __LINE__, + vbox_fb, vbox_fb->obj)); + drm_framebuffer_cleanup(fb); + kfree(fb); +} +void vbox_enable_accel(struct vbox_private *vbox) +{ + unsigned i; + struct VBVABUFFER *vbva; + AssertLogRelReturnVoid(vbox->vbva_info != NULL); + for (i = 0; i < vbox->num_crtcs; ++i) { + if (vbox->vbva_info[i].pVBVA == NULL) { + LogFunc(("vboxvideo: enabling VBVA.\n")); + vbva = (struct VBVABUFFER *) ( ((uint8_t *)vbox->vram) + + vbox->vram_size + + i * VBVA_MIN_BUFFER_SIZE); + if (!VBoxVBVAEnable(&vbox->vbva_info[i], &vbox->submit_info, vbva, i)) + AssertReleaseMsgFailed(("VBoxVBVAEnable failed - heap allocation error, very old host or driver error.\n")); + } + } +} +void vbox_disable_accel(struct vbox_private *vbox) +{ + unsigned i; + for (i = 0; i < vbox->num_crtcs; ++i) + VBoxVBVADisable(&vbox->vbva_info[i], &vbox->submit_info, i); +} +void vbox_enable_caps(struct vbox_private *vbox) +{ + uint32_t caps = VBVACAPS_DISABLE_CURSOR_INTEGRATION + | VBVACAPS_IRQ + | VBVACAPS_USE_VBVA_ONLY; + if (vbox->initial_mode_queried) + caps |= VBVACAPS_VIDEO_MODE_HINTS; + VBoxHGSMISendCapsInfo(&vbox->submit_info, caps); +} +/** Send information about dirty rectangles to VBVA. If necessary we enable + * VBVA first, as this is normally disabled after a change of master in case + * the new master does not send dirty rectangle information (is this even + * allowed?) */ +void vbox_framebuffer_dirty_rectangles(struct drm_framebuffer *fb, + struct drm_clip_rect *rects, + unsigned num_rects) +{ + struct vbox_private *vbox = fb->dev->dev_private; + unsigned i; + LogFunc(("vboxvideo: %d: fb=%p, num_rects=%u, vbox=%p\n", __LINE__, fb, + num_rects, vbox)); + vbox_enable_accel(vbox); + mutex_lock(&vbox->hw_mutex); + for (i = 0; i < num_rects; ++i) + { + struct drm_crtc *crtc; + list_for_each_entry(crtc, &fb->dev->mode_config.crtc_list, head) + { + unsigned crtc_id = to_vbox_crtc(crtc)->crtc_id; + VBVACMDHDR cmd_hdr; + if ( CRTC_FB(crtc) != fb + || rects[i].x1 > crtc->x + + crtc->hwmode.hdisplay + || rects[i].y1 > crtc->y + + crtc->hwmode.vdisplay + || rects[i].x2 < crtc->x + || rects[i].y2 < crtc->y) + continue; + cmd_hdr.x = (int16_t)rects[i].x1; + cmd_hdr.y = (int16_t)rects[i].y1; + cmd_hdr.w = (uint16_t)rects[i].x2 - rects[i].x1; + cmd_hdr.h = (uint16_t)rects[i].y2 - rects[i].y1; + if (VBoxVBVABufferBeginUpdate(&vbox->vbva_info[crtc_id], + &vbox->submit_info)) + { + VBoxVBVAWrite(&vbox->vbva_info[crtc_id], &vbox->submit_info, &cmd_hdr, + sizeof(cmd_hdr)); + VBoxVBVABufferEndUpdate(&vbox->vbva_info[crtc_id]); + } + } + } + mutex_unlock(&vbox->hw_mutex); + LogFunc(("vboxvideo: %d\n", __LINE__)); +} +static int vbox_user_framebuffer_dirty(struct drm_framebuffer *fb, + struct drm_file *file_priv, + unsigned flags, unsigned color, + struct drm_clip_rect *rects, + unsigned num_rects) +{ + vbox_framebuffer_dirty_rectangles(fb, rects, num_rects); + return 0; +} +static const struct drm_framebuffer_funcs vbox_fb_funcs = { + .destroy = vbox_user_framebuffer_destroy, + .dirty = vbox_user_framebuffer_dirty, +}; +int vbox_framebuffer_init(struct drm_device *dev, + struct vbox_framebuffer *vbox_fb, +#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 5, 0) + const +#endif + struct DRM_MODE_FB_CMD *mode_cmd, + struct drm_gem_object *obj) +{ + int ret; + LogFunc(("vboxvideo: %d: dev=%p, vbox_fb=%p, obj=%p\n", __LINE__, dev, + vbox_fb, obj)); + drm_helper_mode_fill_fb_struct(&vbox_fb->base, mode_cmd); + vbox_fb->obj = obj; + ret = drm_framebuffer_init(dev, &vbox_fb->base, &vbox_fb_funcs); + if (ret) { + DRM_ERROR("framebuffer init failed %d\n", ret); + LogFunc(("vboxvideo: %d\n", __LINE__)); + return ret; + } + LogFunc(("vboxvideo: %d\n", __LINE__)); + return 0; +} +static struct drm_framebuffer * +vbox_user_framebuffer_create(struct drm_device *dev, + struct drm_file *filp, +#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 5, 0) + const +#endif + struct drm_mode_fb_cmd2 *mode_cmd) +{ + struct drm_gem_object *obj; + struct vbox_framebuffer *vbox_fb; + int ret; + LogFunc(("vboxvideo: %d\n", __LINE__)); +#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 7, 0) + obj = drm_gem_object_lookup(filp, mode_cmd->handles[0]); +#else + obj = drm_gem_object_lookup(dev, filp, mode_cmd->handles[0]); +#endif + if (obj == NULL) + return ERR_PTR(-ENOENT); + vbox_fb = kzalloc(sizeof(*vbox_fb), GFP_KERNEL); + if (!vbox_fb) { + drm_gem_object_unreference_unlocked(obj); + return ERR_PTR(-ENOMEM); + } + ret = vbox_framebuffer_init(dev, vbox_fb, mode_cmd, obj); + if (ret) { + drm_gem_object_unreference_unlocked(obj); + kfree(vbox_fb); + return ERR_PTR(ret); + } + LogFunc(("vboxvideo: %d\n", __LINE__)); + return &vbox_fb->base; +} +static const struct drm_mode_config_funcs vbox_mode_funcs = { + .fb_create = vbox_user_framebuffer_create, +}; +static void vbox_accel_fini(struct vbox_private *vbox) +{ + if (vbox->vbva_info) + { + vbox_disable_accel(vbox); + kfree(vbox->vbva_info); + vbox->vbva_info = NULL; + } +} +static int vbox_accel_init(struct vbox_private *vbox) +{ + unsigned i; + LogFunc(("vboxvideo: %d: vbox=%p, vbox->num_crtcs=%u, vbox->vbva_info=%p\n", + __LINE__, vbox, (unsigned)vbox->num_crtcs, vbox->vbva_info)); + if (!vbox->vbva_info) + { + vbox->vbva_info = kzalloc( sizeof(struct VBVABUFFERCONTEXT) + * vbox->num_crtcs, + GFP_KERNEL); + if (!vbox->vbva_info) + return -ENOMEM; + } + /* Take a command buffer for each screen from the end of usable VRAM. */ + vbox->vram_size -= vbox->num_crtcs * VBVA_MIN_BUFFER_SIZE; + for (i = 0; i < vbox->num_crtcs; ++i) + VBoxVBVASetupBufferContext(&vbox->vbva_info[i], + vbox->vram_size + i * VBVA_MIN_BUFFER_SIZE, + VBVA_MIN_BUFFER_SIZE); + LogFunc(("vboxvideo: %d: vbox->vbva_info=%p, vbox->vram_size=%u\n", + __LINE__, vbox->vbva_info, (unsigned)vbox->vram_size)); + return 0; +} +/** Allocation function for the HGSMI heap and data. */ +static DECLCALLBACK(void *) alloc_hgsmi_environ(void *environ, HGSMISIZE size) +{ + NOREF(environ); + return kmalloc(size, GFP_KERNEL); +} +/** Free function for the HGSMI heap and data. */ +static DECLCALLBACK(void) free_hgsmi_environ(void *environ, void *ptr) +{ + NOREF(environ); + kfree(ptr); +} +/** Pointers to the HGSMI heap and data manipulation functions. */ +static HGSMIENV hgsmi_environ = +{ + NULL, + alloc_hgsmi_environ, + free_hgsmi_environ +}; +/** Do we support the 4.3 plus mode hint reporting interface? */ +static bool have_hgsmi_mode_hints(struct vbox_private *vbox) +{ + uint32_t have_hints, have_cursor; + return RT_SUCCESS(VBoxQueryConfHGSMI(&vbox->submit_info, VBOX_VBVA_CONF32_MODE_HINT_REPORTING, &have_hints)) + && RT_SUCCESS(VBoxQueryConfHGSMI(&vbox->submit_info, VBOX_VBVA_CONF32_GUEST_CURSOR_REPORTING, &have_cursor)) + && have_hints == VINF_SUCCESS + && have_cursor == VINF_SUCCESS; +} +/** Set up our heaps and data exchange buffers in VRAM before handing the rest + * to the memory manager. */ +static int vbox_hw_init(struct vbox_private *vbox) +{ + uint32_t base_offset, guest_heap_offset, guest_heap_size, host_flags_offset; + void *guest_heap; + vbox->full_vram_size = VBoxVideoGetVRAMSize(); + vbox->any_pitch = VBoxVideoAnyWidthAllowed(); + DRM_INFO("VRAM %08x\n", vbox->full_vram_size); + VBoxHGSMIGetBaseMappingInfo(vbox->full_vram_size, &base_offset, NULL, + &guest_heap_offset, &guest_heap_size, &host_flags_offset); + guest_heap = ((uint8_t *)vbox->vram) + base_offset + guest_heap_offset; + vbox->host_flags_offset = base_offset + host_flags_offset; + if (RT_FAILURE(VBoxHGSMISetupGuestContext(&vbox->submit_info, guest_heap, + guest_heap_size, + base_offset + guest_heap_offset, + &hgsmi_environ))) + return -ENOMEM; + /* Reduce available VRAM size to reflect the guest heap. */ + vbox->vram_size = base_offset; + /* Linux drm represents monitors as a 32-bit array. */ + vbox->num_crtcs = RT_MIN(VBoxHGSMIGetMonitorCount(&vbox->submit_info), 32); + if (!have_hgsmi_mode_hints(vbox)) + return -ENOTSUPP; + vbox->last_mode_hints = kzalloc(sizeof(VBVAMODEHINT) * vbox->num_crtcs, GFP_KERNEL); + if (!vbox->last_mode_hints) + return -ENOMEM; + return vbox_accel_init(vbox); +} +static void vbox_hw_fini(struct vbox_private *vbox) +{ + vbox_accel_fini(vbox); + if (vbox->last_mode_hints) + kfree(vbox->last_mode_hints); + vbox->last_mode_hints = NULL; +} +int vbox_driver_load(struct drm_device *dev, unsigned long flags) +{ + struct vbox_private *vbox; + int ret = 0; + LogFunc(("vboxvideo: %d: dev=%p\n", __LINE__, dev)); + if (!VBoxHGSMIIsSupported()) + return -ENODEV; + vbox = kzalloc(sizeof(struct vbox_private), GFP_KERNEL); + if (!vbox) + return -ENOMEM; + dev->dev_private = vbox; + vbox->dev = dev; + mutex_init(&vbox->hw_mutex); + /* I hope this won't interfere with the memory manager. */ + vbox->vram = pci_iomap(dev->pdev, 0, 0); + if (!vbox->vram) { + ret = -EIO; + goto out_free; + } + ret = vbox_hw_init(vbox); + if (ret) + goto out_free; + ret = vbox_mm_init(vbox); + if (ret) + goto out_free; + drm_mode_config_init(dev); + dev->mode_config.funcs = (void *)&vbox_mode_funcs; + dev->mode_config.min_width = 64; + dev->mode_config.min_height = 64; + dev->mode_config.preferred_depth = 24; + dev->mode_config.max_width = VBE_DISPI_MAX_XRES; + dev->mode_config.max_height = VBE_DISPI_MAX_YRES; + ret = vbox_mode_init(dev); + if (ret) + goto out_free; + ret = vbox_irq_init(vbox); + if (ret) + goto out_free; + ret = vbox_fbdev_init(dev); + if (ret) + goto out_free; + LogFunc(("vboxvideo: %d: vbox=%p, vbox->vram=%p, vbox->full_vram_size=%u\n", + __LINE__, vbox, vbox->vram, (unsigned)vbox->full_vram_size)); + return 0; +out_free: + vbox_driver_unload(dev); + LogFunc(("vboxvideo: %d: ret=%d\n", __LINE__, ret)); + return ret; +} +int vbox_driver_unload(struct drm_device *dev) +{ + struct vbox_private *vbox = dev->dev_private; + LogFunc(("vboxvideo: %d\n", __LINE__)); + vbox_fbdev_fini(dev); + vbox_irq_fini(vbox); + vbox_mode_fini(dev); + if (dev->mode_config.funcs) + drm_mode_config_cleanup(dev); + vbox_hw_fini(vbox); + vbox_mm_fini(vbox); + if (vbox->vram) + pci_iounmap(dev->pdev, vbox->vram); + kfree(vbox); + dev->dev_private = NULL; + LogFunc(("vboxvideo: %d\n", __LINE__)); + return 0; +} +/** @note this is described in the DRM framework documentation. AST does not + * have it, but we get an oops on driver unload if it is not present. */ +void vbox_driver_lastclose(struct drm_device *dev) +{ + struct vbox_private *vbox = dev->dev_private; +#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 16, 0) + if (vbox->fbdev) + drm_fb_helper_restore_fbdev_mode_unlocked(&vbox->fbdev->helper); +#else + drm_modeset_lock_all(dev); + if (vbox->fbdev) + drm_fb_helper_restore_fbdev_mode(&vbox->fbdev->helper); + drm_modeset_unlock_all(dev); +#endif +} +int vbox_gem_create(struct drm_device *dev, + u32 size, bool iskernel, + struct drm_gem_object **obj) +{ + struct vbox_bo *vboxbo; + int ret; + LogFunc(("vboxvideo: %d: dev=%p, size=%u, iskernel=%u\n", __LINE__, + dev, (unsigned)size, (unsigned)iskernel)); + *obj = NULL; + size = roundup(size, PAGE_SIZE); + if (size == 0) + return -EINVAL; + ret = vbox_bo_create(dev, size, 0, 0, &vboxbo); + if (ret) { + if (ret != -ERESTARTSYS) + DRM_ERROR("failed to allocate GEM object\n"); + return ret; + } + *obj = &vboxbo->gem; + LogFunc(("vboxvideo: %d: obj=%p\n", __LINE__, obj)); + return 0; +} +int vbox_dumb_create(struct drm_file *file, + struct drm_device *dev, + struct drm_mode_create_dumb *args) +{ + int ret; + struct drm_gem_object *gobj; + u32 handle; + LogFunc(("vboxvideo: %d: args->width=%u, args->height=%u, args->bpp=%u\n", + __LINE__, (unsigned)args->width, (unsigned)args->height, + (unsigned)args->bpp)); + args->pitch = args->width * ((args->bpp + 7) / 8); + args->size = args->pitch * args->height; + ret = vbox_gem_create(dev, args->size, false, + &gobj); + if (ret) + return ret; + ret = drm_gem_handle_create(file, gobj, &handle); + drm_gem_object_unreference_unlocked(gobj); + if (ret) + return ret; + args->handle = handle; + LogFunc(("vboxvideo: %d: args->handle=%u\n", __LINE__, + (unsigned)args->handle)); + return 0; +} +#if LINUX_VERSION_CODE < KERNEL_VERSION(3, 12, 0) +int vbox_dumb_destroy(struct drm_file *file, + struct drm_device *dev, + uint32_t handle) +{ + LogFunc(("vboxvideo: %d: dev=%p, handle=%u\n", __LINE__, dev, + (unsigned)handle)); + return drm_gem_handle_delete(file, handle); +} +#endif +static void vbox_bo_unref(struct vbox_bo **bo) +{ + struct ttm_buffer_object *tbo; + if ((*bo) == NULL) + return; + LogFunc(("vboxvideo: %d: bo=%p\n", __LINE__, bo)); + tbo = &((*bo)->bo); + ttm_bo_unref(&tbo); + if (tbo == NULL) + *bo = NULL; +} +void vbox_gem_free_object(struct drm_gem_object *obj) +{ + struct vbox_bo *vbox_bo = gem_to_vbox_bo(obj); + LogFunc(("vboxvideo: %d: vbox_bo=%p\n", __LINE__, vbox_bo)); + vbox_bo_unref(&vbox_bo); +} +static inline u64 vbox_bo_mmap_offset(struct vbox_bo *bo) +{ +#if LINUX_VERSION_CODE < KERNEL_VERSION(3, 12, 0) + return bo->bo.addr_space_offset; +#else + return drm_vma_node_offset_addr(&bo->bo.vma_node); +#endif +} +int +vbox_dumb_mmap_offset(struct drm_file *file, + struct drm_device *dev, + uint32_t handle, + uint64_t *offset) +{ + struct drm_gem_object *obj; + int ret; + struct vbox_bo *bo; + LogFunc(("vboxvideo: %d: dev=%p, handle=%u\n", __LINE__, + dev, (unsigned)handle)); + mutex_lock(&dev->struct_mutex); +#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 7, 0) + obj = drm_gem_object_lookup(file, handle); +#else + obj = drm_gem_object_lookup(dev, file, handle); +#endif + if (obj == NULL) { + ret = -ENOENT; + goto out_unlock; + } + bo = gem_to_vbox_bo(obj); + *offset = vbox_bo_mmap_offset(bo); + drm_gem_object_unreference(obj); + ret = 0; + LogFunc(("vboxvideo: %d: bo=%p, *offset=%llu\n", __LINE__, + bo, (unsigned long long)*offset)); +out_unlock: + mutex_unlock(&dev->struct_mutex); + return ret; +}
\ No newline at end of file diff --git a/vbox_mode.c b/vbox_mode.c new file mode 100644 index 000000000000..5619c9431ff5 --- /dev/null +++ b/vbox_mode.c @@ -0,0 +1,720 @@ +/* $Id$ */ +/** @file + * VirtualBox Additions Linux kernel video driver + */ +/* + * Copyright (C) 2013 Oracle Corporation + * + * This file is part of VirtualBox Open Source Edition (OSE), as + * available from http://www.virtualbox.org. This file is free software; + * you can redistribute it and/or modify it under the terms of the GNU + * General Public License (GPL) as published by the Free Software + * Foundation, in version 2 as it comes in the "COPYING" file of the + * VirtualBox OSE distribution. VirtualBox OSE is distributed in the + * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind. + * -------------------------------------------------------------------- + * + * This code is based on + * ast_mode.c + * with the following copyright and permission notice: + * + * Copyright 2012 Red Hat Inc. + * Parts based on xf86-video-ast + * Copyright (c) 2005 ASPEED Technology Inc. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sub license, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL + * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR + * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE + * USE OR OTHER DEALINGS IN THE SOFTWARE. + * + * The above copyright notice and this permission notice (including the + * next paragraph) shall be included in all copies or substantial portions + * of the Software. + * + */ +/* + * Authors: Dave Airlie <airlied@redhat.com> + */ +#include "vbox_drv.h" +#include <VBox/VBoxVideo.h> +#include <linux/export.h> +#include <drm/drm_crtc_helper.h> +#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 18, 0) +# include <drm/drm_plane_helper.h> +#endif +static int vbox_cursor_set2(struct drm_crtc *crtc, struct drm_file *file_priv, + uint32_t handle, uint32_t width, uint32_t height, + int32_t hot_x, int32_t hot_y); +static int vbox_cursor_move(struct drm_crtc *crtc, int x, int y); +/** Set a graphics mode. Poke any required values into registers, do an HGSMI + * mode set and tell the host we support advanced graphics functions. + */ +static void vbox_do_modeset(struct drm_crtc *crtc, + const struct drm_display_mode *mode) +{ + struct vbox_crtc *vbox_crtc = to_vbox_crtc(crtc); + struct vbox_private *vbox; + int width, height, bpp, pitch; + unsigned crtc_id; + uint16_t flags; + LogFunc(("vboxvideo: %d: vbox_crtc=%p, CRTC_FB(crtc)=%p\n", __LINE__, + vbox_crtc, CRTC_FB(crtc))); + vbox = crtc->dev->dev_private; + width = mode->hdisplay ? mode->hdisplay : 640; + height = mode->vdisplay ? mode->vdisplay : 480; + crtc_id = vbox_crtc->crtc_id; + bpp = crtc->enabled ? CRTC_FB(crtc)->bits_per_pixel : 32; +#if LINUX_VERSION_CODE < KERNEL_VERSION(3, 3, 0) + pitch = crtc->enabled ? CRTC_FB(crtc)->pitch : width * bpp / 8; +#else + pitch = crtc->enabled ? CRTC_FB(crtc)->pitches[0] : width * bpp / 8; +#endif + /* This is the old way of setting graphics modes. It assumed one screen + * and a frame-buffer at the start of video RAM. On older versions of + * VirtualBox, certain parts of the code still assume that the first + * screen is programmed this way, so try to fake it. */ + if ( vbox_crtc->crtc_id == 0 + && crtc->enabled + && vbox_crtc->fb_offset / pitch < 0xffff - crtc->y + && vbox_crtc->fb_offset % (bpp / 8) == 0) + VBoxVideoSetModeRegisters(width, height, pitch * 8 / bpp, + CRTC_FB(crtc)->bits_per_pixel, 0, + vbox_crtc->fb_offset % pitch / bpp * 8 + crtc->x, + vbox_crtc->fb_offset / pitch + crtc->y); + flags = VBVA_SCREEN_F_ACTIVE; + flags |= (crtc->enabled && !vbox_crtc->blanked ? 0 : VBVA_SCREEN_F_BLANK); + flags |= (vbox_crtc->disconnected ? VBVA_SCREEN_F_DISABLED : 0); + VBoxHGSMIProcessDisplayInfo(&vbox->submit_info, vbox_crtc->crtc_id, + crtc->x, crtc->y, + crtc->x * bpp / 8 + crtc->y * pitch, + pitch, width, height, + vbox_crtc->blanked ? 0 : bpp, flags); + VBoxHGSMIReportFlagsLocation(&vbox->submit_info, vbox->host_flags_offset); + LogFunc(("vboxvideo: %d\n", __LINE__)); +} +static int vbox_set_view(struct drm_crtc *crtc) +{ + struct vbox_crtc *vbox_crtc = to_vbox_crtc(crtc); + struct vbox_private *vbox = crtc->dev->dev_private; + void *p; + LogFunc(("vboxvideo: %d: vbox_crtc=%p\n", __LINE__, vbox_crtc)); + /* Tell the host about the view. This design originally targeted the + * Windows XP driver architecture and assumed that each screen would have + * a dedicated frame buffer with the command buffer following it, the whole + * being a "view". The host works out which screen a command buffer belongs + * to by checking whether it is in the first view, then whether it is in the + * second and so on. The first match wins. We cheat around this by making + * the first view be the managed memory plus the first command buffer, the + * second the same plus the second buffer and so on. */ + p = VBoxHGSMIBufferAlloc(&vbox->submit_info, sizeof(VBVAINFOVIEW), HGSMI_CH_VBVA, + VBVA_INFO_VIEW); + if (p) + { + VBVAINFOVIEW *pInfo = (VBVAINFOVIEW *)p; + pInfo->u32ViewIndex = vbox_crtc->crtc_id; + pInfo->u32ViewOffset = vbox_crtc->fb_offset; + pInfo->u32ViewSize = vbox->vram_size - vbox_crtc->fb_offset + + vbox_crtc->crtc_id * VBVA_MIN_BUFFER_SIZE; + pInfo->u32MaxScreenSize = vbox->vram_size - vbox_crtc->fb_offset; + VBoxHGSMIBufferSubmit(&vbox->submit_info, p); + VBoxHGSMIBufferFree(&vbox->submit_info, p); + } + else + return -ENOMEM; + LogFunc(("vboxvideo: %d: p=%p\n", __LINE__, p)); + return 0; +} +static void vbox_crtc_load_lut(struct drm_crtc *crtc) +{ +} +static void vbox_crtc_dpms(struct drm_crtc *crtc, int mode) +{ + struct vbox_crtc *vbox_crtc = to_vbox_crtc(crtc); + struct vbox_private *vbox = crtc->dev->dev_private; + LogFunc(("vboxvideo: %d: vbox_crtc=%p, mode=%d\n", __LINE__, vbox_crtc, + mode)); + switch (mode) { + case DRM_MODE_DPMS_ON: + vbox_crtc->blanked = false; + break; + case DRM_MODE_DPMS_STANDBY: + case DRM_MODE_DPMS_SUSPEND: + case DRM_MODE_DPMS_OFF: + vbox_crtc->blanked = true; + break; + } + mutex_lock(&vbox->hw_mutex); + vbox_do_modeset(crtc, &crtc->hwmode); + mutex_unlock(&vbox->hw_mutex); + LogFunc(("vboxvideo: %d\n", __LINE__)); +} +static bool vbox_crtc_mode_fixup(struct drm_crtc *crtc, + const struct drm_display_mode *mode, + struct drm_display_mode *adjusted_mode) +{ + return true; +} +/* We move buffers which are not in active use out of VRAM to save memory. */ +static int vbox_crtc_do_set_base(struct drm_crtc *crtc, + struct drm_framebuffer *fb, + int x, int y, int atomic) +{ + struct vbox_private *vbox = crtc->dev->dev_private; + struct vbox_crtc *vbox_crtc = to_vbox_crtc(crtc); + struct drm_gem_object *obj; + struct vbox_framebuffer *vbox_fb; + struct vbox_bo *bo; + int ret; + u64 gpu_addr; + LogFunc(("vboxvideo: %d: fb=%p, vbox_crtc=%p\n", __LINE__, fb, vbox_crtc)); + /* push the previous fb to system ram */ + if (!atomic && fb) { + vbox_fb = to_vbox_framebuffer(fb); + obj = vbox_fb->obj; + bo = gem_to_vbox_bo(obj); + ret = vbox_bo_reserve(bo, false); + if (ret) + return ret; + vbox_bo_push_sysram(bo); + vbox_bo_unreserve(bo); + } + vbox_fb = to_vbox_framebuffer(CRTC_FB(crtc)); + obj = vbox_fb->obj; + bo = gem_to_vbox_bo(obj); + ret = vbox_bo_reserve(bo, false); + if (ret) + return ret; + ret = vbox_bo_pin(bo, TTM_PL_FLAG_VRAM, &gpu_addr); + if (ret) { + vbox_bo_unreserve(bo); + return ret; + } + if (&vbox->fbdev->afb == vbox_fb) { + /* if pushing console in kmap it */ + ret = ttm_bo_kmap(&bo->bo, 0, bo->bo.num_pages, &bo->kmap); + if (ret) + DRM_ERROR("failed to kmap fbcon\n"); + else + vbox_fbdev_set_base(vbox, gpu_addr); + } + vbox_bo_unreserve(bo); + /* vbox_set_start_address_crt1(crtc, (u32)gpu_addr); */ + vbox_crtc->fb_offset = gpu_addr; + if (vbox_crtc->crtc_id == 0) { + vbox->input_mapping_width = CRTC_FB(crtc)->width; + vbox->input_mapping_height = CRTC_FB(crtc)->height; + } + LogFunc(("vboxvideo: %d: vbox_fb=%p, obj=%p, bo=%p, gpu_addr=%u\n", + __LINE__, vbox_fb, obj, bo, (unsigned)gpu_addr)); + return 0; +} +static int vbox_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y, + struct drm_framebuffer *old_fb) +{ + LogFunc(("vboxvideo: %d\n", __LINE__)); + return vbox_crtc_do_set_base(crtc, old_fb, x, y, 0); +} +static int vbox_crtc_mode_set(struct drm_crtc *crtc, + struct drm_display_mode *mode, + struct drm_display_mode *adjusted_mode, + int x, int y, + struct drm_framebuffer *old_fb) +{ + struct vbox_private *vbox = crtc->dev->dev_private; + int rc = 0; + LogFunc(("vboxvideo: %d: vbox=%p\n", __LINE__, vbox)); + vbox_crtc_mode_set_base(crtc, x, y, old_fb); + mutex_lock(&vbox->hw_mutex); + rc = vbox_set_view(crtc); + if (!rc) + vbox_do_modeset(crtc, mode); + /* Note that the input mapping is always relative to the first screen. */ + VBoxHGSMIUpdateInputMapping(&vbox->submit_info, 0, 0, + vbox->input_mapping_width, + vbox->input_mapping_height); + mutex_unlock(&vbox->hw_mutex); + LogFunc(("vboxvideo: %d\n", __LINE__)); + return rc; +} +static void vbox_crtc_disable(struct drm_crtc *crtc) +{ +} +static void vbox_crtc_prepare(struct drm_crtc *crtc) +{ +} +static void vbox_crtc_commit(struct drm_crtc *crtc) +{ +} +static const struct drm_crtc_helper_funcs vbox_crtc_helper_funcs = { + .dpms = vbox_crtc_dpms, + .mode_fixup = vbox_crtc_mode_fixup, + .mode_set = vbox_crtc_mode_set, + /* .mode_set_base = vbox_crtc_mode_set_base, */ + .disable = vbox_crtc_disable, + .load_lut = vbox_crtc_load_lut, + .prepare = vbox_crtc_prepare, + .commit = vbox_crtc_commit, +}; +static void vbox_crtc_reset(struct drm_crtc *crtc) +{ +} +static void vbox_crtc_destroy(struct drm_crtc *crtc) +{ + drm_crtc_cleanup(crtc); + kfree(crtc); +} +static const struct drm_crtc_funcs vbox_crtc_funcs = { + .cursor_move = vbox_cursor_move, + .cursor_set2 = vbox_cursor_set2, + .reset = vbox_crtc_reset, + .set_config = drm_crtc_helper_set_config, + /* .gamma_set = vbox_crtc_gamma_set, */ + .destroy = vbox_crtc_destroy, +}; +static struct vbox_crtc *vbox_crtc_init(struct drm_device *dev, unsigned i) +{ + struct vbox_crtc *vbox_crtc; + LogFunc(("vboxvideo: %d\n", __LINE__)); + vbox_crtc = kzalloc(sizeof(struct vbox_crtc), GFP_KERNEL); + if (!vbox_crtc) + return NULL; + vbox_crtc->crtc_id = i; + drm_crtc_init(dev, &vbox_crtc->base, &vbox_crtc_funcs); + drm_mode_crtc_set_gamma_size(&vbox_crtc->base, 256); + drm_crtc_helper_add(&vbox_crtc->base, &vbox_crtc_helper_funcs); + LogFunc(("vboxvideo: %d: crtc=%p\n", __LINE__, vbox_crtc)); + return vbox_crtc; +} +static void vbox_encoder_destroy(struct drm_encoder *encoder) +{ + LogFunc(("vboxvideo: %d: encoder=%p\n", __LINE__, encoder)); + drm_encoder_cleanup(encoder); + kfree(encoder); +} +#if LINUX_VERSION_CODE < KERNEL_VERSION(3, 13, 0) +static struct drm_encoder *drm_encoder_find(struct drm_device *dev, uint32_t id) +{ + struct drm_mode_object *mo; + mo = drm_mode_object_find(dev, id, DRM_MODE_OBJECT_ENCODER); + return mo ? obj_to_encoder(mo) : NULL; +} +#endif +static struct drm_encoder *vbox_best_single_encoder(struct drm_connector *connector) +{ + int enc_id = connector->encoder_ids[0]; + LogFunc(("vboxvideo: %d: connector=%p\n", __LINE__, connector)); + /* pick the encoder ids */ + if (enc_id) + return drm_encoder_find(connector->dev, enc_id); + LogFunc(("vboxvideo: %d\n", __LINE__)); + return NULL; +} +static const struct drm_encoder_funcs vbox_enc_funcs = { + .destroy = vbox_encoder_destroy, +}; +static void vbox_encoder_dpms(struct drm_encoder *encoder, int mode) +{ +} +static bool vbox_mode_fixup(struct drm_encoder *encoder, + const struct drm_display_mode *mode, + struct drm_display_mode *adjusted_mode) +{ + return true; +} +static void vbox_encoder_mode_set(struct drm_encoder *encoder, + struct drm_display_mode *mode, + struct drm_display_mode *adjusted_mode) +{ +} +static void vbox_encoder_prepare(struct drm_encoder *encoder) +{ +} +static void vbox_encoder_commit(struct drm_encoder *encoder) +{ +} +static const struct drm_encoder_helper_funcs vbox_enc_helper_funcs = { + .dpms = vbox_encoder_dpms, + .mode_fixup = vbox_mode_fixup, + .prepare = vbox_encoder_prepare, + .commit = vbox_encoder_commit, + .mode_set = vbox_encoder_mode_set, +}; +static struct drm_encoder *vbox_encoder_init(struct drm_device *dev, unsigned i) +{ + struct vbox_encoder *vbox_encoder; + LogFunc(("vboxvideo: %d: dev=%d\n", __LINE__)); + vbox_encoder = kzalloc(sizeof(struct vbox_encoder), GFP_KERNEL); + if (!vbox_encoder) + return NULL; + drm_encoder_init(dev, &vbox_encoder->base, &vbox_enc_funcs, + DRM_MODE_ENCODER_DAC +#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 5, 0) + , NULL +#endif + ); + drm_encoder_helper_add(&vbox_encoder->base, &vbox_enc_helper_funcs); + vbox_encoder->base.possible_crtcs = 1 << i; + LogFunc(("vboxvideo: %d: vbox_encoder=%p\n", __LINE__, vbox_encoder)); + return &vbox_encoder->base; +} +/** Generate EDID data with a mode-unique serial number for the virtual + * monitor to try to persuade Unity that different modes correspond to + * different monitors and it should not try to force the same resolution on + * them. */ +static void vbox_set_edid(struct drm_connector *connector, int width, + int height) +{ + enum { EDID_SIZE = 128 }; + unsigned char edid[EDID_SIZE] = { + 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, /* header */ + 0x58, 0x58, /* manufacturer (VBX) */ + 0x00, 0x00, /* product code */ + 0x00, 0x00,0x00, 0x00, /* serial number goes here */ + 0x01, /* week of manufacture */ + 0x00, /* year of manufacture */ + 0x01, 0x03, /* EDID version */ + 0x80, /* capabilities - digital */ + 0x00, /* horiz. res in cm, zero for projectors */ + 0x00, /* vert. res in cm */ + 0x78, /* display gamma (120 == 2.2). */ + 0xEE, /* features (standby, suspend, off, RGB, standard colour space, + * preferred timing mode) */ + 0xEE, 0x91, 0xA3, 0x54, 0x4C, 0x99, 0x26, 0x0F, 0x50, 0x54, + /* chromaticity for standard colour space. */ + 0x00, 0x00, 0x00, /* no default timings */ + 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, + 0x01, 0x01, 0x01, 0x01, /* no standard timings */ + 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x06, 0x00, 0x02, 0x02, 0x02, 0x02, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* descriptor block 1 goes here */ + 0x00, 0x00, 0x00, 0xFD, 0x00, /* descriptor block 2, monitor ranges */ + 0x00, 0xC8, 0x00, 0xC8, 0x64, 0x00, 0x0A, 0x20, 0x20, 0x20, 0x20, 0x20, + 0x20, /* 0-200Hz vertical, 0-200KHz horizontal, 1000MHz pixel clock */ + 0x00, 0x00, 0x00, 0xFC, 0x00, /* descriptor block 3, monitor name */ + 'V', 'B', 'O', 'X', ' ', 'm', 'o', 'n', 'i', 't', 'o', 'r', '\n', + 0x00, 0x00, 0x00, 0x10, 0x00, /* descriptor block 4: dummy data */ + 0x0A, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, + 0x20, + 0x00, /* number of extensions */ + 0x00 /* checksum goes here */ + }; + int clock = (width + 6) * (height + 6) * 60 / 10000; + unsigned i; + unsigned sum = 0; + edid[12] = width & 0xff; + edid[13] = width >> 8; + edid[14] = height & 0xff; + edid[15] = height >> 8; + edid[54] = clock & 0xff; + edid[55] = clock >> 8; + edid[56] = width & 0xff; + edid[58] = (width >> 4) & 0xf0; + edid[59] = height & 0xff; + edid[61] = (height >> 4) & 0xf0; + for (i = 0; i < EDID_SIZE - 1; ++i) + sum += edid[i]; + edid[EDID_SIZE - 1] = (0x100 - (sum & 0xFF)) & 0xFF; + drm_mode_connector_update_edid_property(connector, (struct edid *)edid); +} +static int vbox_get_modes(struct drm_connector *connector) +{ + struct vbox_connector *vbox_connector = NULL; + struct drm_display_mode *mode = NULL; + struct vbox_private *vbox = NULL; + unsigned num_modes = 0; + int preferred_width, preferred_height; + LogFunc(("vboxvideo: %d: connector=%p\n", __LINE__, connector)); + vbox_connector = to_vbox_connector(connector); + vbox = connector->dev->dev_private; + if (vbox_connector->vbox_crtc->crtc_id == 0) + vbox_enable_caps(vbox); + if (!vbox->initial_mode_queried) { + if (vbox_connector->vbox_crtc->crtc_id == 0) { + vbox->initial_mode_queried = true; + vbox_report_hotplug(vbox); + } + return drm_add_modes_noedid(connector, 800, 600); + } + num_modes = drm_add_modes_noedid(connector, 2560, 1600); + preferred_width = vbox_connector->mode_hint.width ? vbox_connector->mode_hint.width : 1024; + preferred_height = vbox_connector->mode_hint.height ? vbox_connector->mode_hint.height : 768; + mode = drm_cvt_mode(connector->dev, preferred_width, preferred_height, 60, false, + false, false); + if (mode) + { + mode->type |= DRM_MODE_TYPE_PREFERRED; + drm_mode_probed_add(connector, mode); + ++num_modes; + } + vbox_set_edid(connector, preferred_width, preferred_height); + return num_modes; +} +static int vbox_mode_valid(struct drm_connector *connector, + struct drm_display_mode *mode) +{ + return MODE_OK; +} +static void vbox_connector_destroy(struct drm_connector *connector) +{ + struct vbox_connector *vbox_connector = NULL; + LogFunc(("vboxvideo: %d: connector=%p\n", __LINE__, connector)); + vbox_connector = to_vbox_connector(connector); +#if LINUX_VERSION_CODE < KERNEL_VERSION(3, 17, 0) + drm_sysfs_connector_remove(connector); +#else + drm_connector_unregister(connector); +#endif + drm_connector_cleanup(connector); + kfree(connector); +} +static enum drm_connector_status +vbox_connector_detect(struct drm_connector *connector, bool force) +{ + struct vbox_connector *vbox_connector = NULL; + (void) force; + LogFunc(("vboxvideo: %d: connector=%p\n", __LINE__, connector)); + vbox_connector = to_vbox_connector(connector); + return vbox_connector->mode_hint.disconnected ? + connector_status_disconnected : connector_status_connected; +} +static int vbox_fill_modes(struct drm_connector *connector, uint32_t max_x, uint32_t max_y) +{ + struct vbox_connector *vbox_connector; + struct drm_device *dev; + struct drm_display_mode *mode, *iterator; + LogFunc(("vboxvideo: %d: connector=%p, max_x=%lu, max_y = %lu\n", __LINE__, + connector, (unsigned long)max_x, (unsigned long)max_y)); + vbox_connector = to_vbox_connector(connector); + dev = vbox_connector->base.dev; + list_for_each_entry_safe(mode, iterator, &connector->modes, head) + { + list_del(&mode->head); + drm_mode_destroy(dev, mode); + } + return drm_helper_probe_single_connector_modes(connector, max_x, max_y); +} +static const struct drm_connector_helper_funcs vbox_connector_helper_funcs = { + .mode_valid = vbox_mode_valid, + .get_modes = vbox_get_modes, + .best_encoder = vbox_best_single_encoder, +}; +static const struct drm_connector_funcs vbox_connector_funcs = { + .dpms = drm_helper_connector_dpms, + .detect = vbox_connector_detect, + .fill_modes = vbox_fill_modes, + .destroy = vbox_connector_destroy, +}; +static int vbox_connector_init(struct drm_device *dev, + struct vbox_crtc *vbox_crtc, + struct drm_encoder *encoder) +{ + struct vbox_connector *vbox_connector; + struct drm_connector *connector; + LogFunc(("vboxvideo: %d: dev=%p, encoder=%p\n", __LINE__, dev, + encoder)); + vbox_connector = kzalloc(sizeof(struct vbox_connector), GFP_KERNEL); + if (!vbox_connector) + return -ENOMEM; + connector = &vbox_connector->base; + vbox_connector->vbox_crtc = vbox_crtc; + drm_connector_init(dev, connector, &vbox_connector_funcs, + DRM_MODE_CONNECTOR_VGA); + drm_connector_helper_add(connector, &vbox_connector_helper_funcs); + connector->interlace_allowed = 0; + connector->doublescan_allowed = 0; +#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 19, 0) + drm_mode_create_suggested_offset_properties(dev); + drm_object_attach_property(&connector->base, + dev->mode_config.suggested_x_property, 0); + drm_object_attach_property(&connector->base, + dev->mode_config.suggested_y_property, 0); +#endif +#if LINUX_VERSION_CODE < KERNEL_VERSION(3, 17, 0) + drm_sysfs_connector_add(connector); +#else + drm_connector_register(connector); +#endif + drm_mode_connector_attach_encoder(connector, encoder); + LogFunc(("vboxvideo: %d: connector=%p\n", __LINE__, connector)); + return 0; +} +int vbox_mode_init(struct drm_device *dev) +{ + struct vbox_private *vbox = dev->dev_private; + struct drm_encoder *encoder; + struct vbox_crtc *vbox_crtc; + unsigned i; + /* vbox_cursor_init(dev); */ + LogFunc(("vboxvideo: %d: dev=%p\n", __LINE__, dev)); + for (i = 0; i < vbox->num_crtcs; ++i) + { + vbox_crtc = vbox_crtc_init(dev, i); + if (!vbox_crtc) + return -ENOMEM; + encoder = vbox_encoder_init(dev, i); + if (!encoder) + return -ENOMEM; + vbox_connector_init(dev, vbox_crtc, encoder); + } + return 0; +} +void vbox_mode_fini(struct drm_device *dev) +{ + /* vbox_cursor_fini(dev); */ +} +/** Copy the ARGB image and generate the mask, which is needed in case the host + * does not support ARGB cursors. The mask is a 1BPP bitmap with the bit set + * if the corresponding alpha value in the ARGB image is greater than 0xF0. */ +static void copy_cursor_image(u8 *src, u8 *dst, int width, int height, + size_t mask_size) +{ + unsigned i, j; + size_t line_size = (width + 7) / 8; + memcpy(dst + mask_size, src, width * height * 4); + for (i = 0; i < height; ++i) + for (j = 0; j < width; ++j) + if (((uint32_t *)src)[i * width + j] > 0xf0000000) + dst[i * line_size + j / 8] |= (0x80 >> (j % 8)); +} +static int vbox_cursor_set2(struct drm_crtc *crtc, struct drm_file *file_priv, + uint32_t handle, uint32_t width, uint32_t height, + int32_t hot_x, int32_t hot_y) +{ + struct vbox_private *vbox = crtc->dev->dev_private; + struct vbox_crtc *vbox_crtc = to_vbox_crtc(crtc); + struct drm_gem_object *obj; + struct vbox_bo *bo; + int ret, rc; + struct ttm_bo_kmap_obj uobj_map; + u8 *src; + u8 *dst = NULL; + u32 caps = 0; + size_t data_size, mask_size; + bool src_isiomem; + /* Re-set this regularly as in 5.0.20 and earlier the information was lost + * on save and restore. */ + VBoxHGSMIUpdateInputMapping(&vbox->submit_info, 0, 0, + vbox->input_mapping_width, + vbox->input_mapping_height); + if (!handle) { + bool cursor_enabled = false; + struct drm_crtc *crtci; + /* Hide cursor. */ + vbox_crtc->cursor_enabled = false; + list_for_each_entry(crtci, &vbox->dev->mode_config.crtc_list, head) + if (to_vbox_crtc(crtci)->cursor_enabled) + cursor_enabled = true; + if (!cursor_enabled) + VBoxHGSMIUpdatePointerShape(&vbox->submit_info, 0, 0, 0, 0, 0, NULL, 0); + return 0; + } + vbox_crtc->cursor_enabled = true; + if ( width > VBOX_MAX_CURSOR_WIDTH || height > VBOX_MAX_CURSOR_HEIGHT + || width == 0 || height == 0) + return -EINVAL; + rc = VBoxQueryConfHGSMI(&vbox->submit_info, + VBOX_VBVA_CONF32_CURSOR_CAPABILITIES, &caps); + ret = -RTErrConvertToErrno(rc); + if (ret) + return ret; + if ( caps & VMMDEV_MOUSE_HOST_CANNOT_HWPOINTER + || !(caps & VMMDEV_MOUSE_HOST_WANTS_ABSOLUTE)) + return -EINVAL; +#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 7, 0) + obj = drm_gem_object_lookup(file_priv, handle); +#else + obj = drm_gem_object_lookup(crtc->dev, file_priv, handle); +#endif + if (obj) + { + bo = gem_to_vbox_bo(obj); + ret = vbox_bo_reserve(bo, false); + if (!ret) + { + /* The mask must be calculated based on the alpha channel, one bit + * per ARGB word, and must be 32-bit padded. */ + mask_size = ((width + 7) / 8 * height + 3) & ~3; + data_size = width * height * 4 + mask_size; + vbox->cursor_hot_x = min((uint32_t)max(hot_x, 0), width); + vbox->cursor_hot_y = min((uint32_t)max(hot_y, 0), height); + vbox->cursor_width = width; + vbox->cursor_height = height; + vbox->cursor_data_size = data_size; + dst = vbox->cursor_data; + ret = ttm_bo_kmap(&bo->bo, 0, bo->bo.num_pages, &uobj_map); + if (!ret) + { + src = ttm_kmap_obj_virtual(&uobj_map, &src_isiomem); + if (!src_isiomem) + { + uint32_t flags = VBOX_MOUSE_POINTER_VISIBLE + | VBOX_MOUSE_POINTER_SHAPE + | VBOX_MOUSE_POINTER_ALPHA; + copy_cursor_image(src, dst, width, height, mask_size); + rc = VBoxHGSMIUpdatePointerShape(&vbox->submit_info, flags, + vbox->cursor_hot_x, + vbox->cursor_hot_y, + width, height, dst, + data_size); + ret = -RTErrConvertToErrno(rc); + } + else + DRM_ERROR("src cursor bo should be in main memory\n"); + ttm_bo_kunmap(&uobj_map); + } + else + vbox->cursor_data_size = 0; + vbox_bo_unreserve(bo); + } + drm_gem_object_unreference_unlocked(obj); + } + else + { + DRM_ERROR("Cannot find cursor object %x for crtc\n", handle); + ret = -ENOENT; + } + return ret; +} +static int vbox_cursor_move(struct drm_crtc *crtc, + int x, int y) +{ + struct vbox_private *vbox = crtc->dev->dev_private; + uint32_t flags = VBOX_MOUSE_POINTER_VISIBLE + | VBOX_MOUSE_POINTER_SHAPE + | VBOX_MOUSE_POINTER_ALPHA; + uint32_t host_x, host_y; + uint32_t hot_x = 0; + uint32_t hot_y = 0; + int rc; + /* We compare these to unsigned later and don't need to handle negative. */ + if (x + crtc->x < 0 || y + crtc->y < 0 || vbox->cursor_data_size == 0) + return 0; + rc = VBoxHGSMICursorPosition(&vbox->submit_info, true, x + crtc->x, + y + crtc->y, &host_x, &host_y); + /* Work around a bug after save and restore in 5.0.20 and earlier. */ + if (RT_FAILURE(rc) || (host_x == 0 && host_y == 0)) + return -RTErrConvertToErrno(rc); + if (x + crtc->x < host_x) + hot_x = min(host_x - x - crtc->x, vbox->cursor_width); + if (y + crtc->y < host_y) + hot_y = min(host_y - y - crtc->y, vbox->cursor_height); + if (hot_x == vbox->cursor_hot_x && hot_y == vbox->cursor_hot_y) + return 0; + vbox->cursor_hot_x = hot_x; + vbox->cursor_hot_y = hot_y; + rc = VBoxHGSMIUpdatePointerShape(&vbox->submit_info, flags, hot_x, hot_y, + vbox->cursor_width, vbox->cursor_height, + vbox->cursor_data, + vbox->cursor_data_size); + return -RTErrConvertToErrno(rc); +}
\ No newline at end of file |