4cedb1a5cf9082f72e6b8373e0dc8a15e312ea46vboxsync/** @file $Id$
4cedb1a5cf9082f72e6b8373e0dc8a15e312ea46vboxsync * VirtualBox Additions Linux kernel video driver
4cedb1a5cf9082f72e6b8373e0dc8a15e312ea46vboxsync * Copyright (C) 2013 Oracle Corporation
4cedb1a5cf9082f72e6b8373e0dc8a15e312ea46vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
4cedb1a5cf9082f72e6b8373e0dc8a15e312ea46vboxsync * available from http://www.virtualbox.org. This file is free software;
4cedb1a5cf9082f72e6b8373e0dc8a15e312ea46vboxsync * you can redistribute it and/or modify it under the terms of the GNU
4cedb1a5cf9082f72e6b8373e0dc8a15e312ea46vboxsync * General Public License (GPL) as published by the Free Software
4cedb1a5cf9082f72e6b8373e0dc8a15e312ea46vboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
4cedb1a5cf9082f72e6b8373e0dc8a15e312ea46vboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
4cedb1a5cf9082f72e6b8373e0dc8a15e312ea46vboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
4cedb1a5cf9082f72e6b8373e0dc8a15e312ea46vboxsync * --------------------------------------------------------------------
4cedb1a5cf9082f72e6b8373e0dc8a15e312ea46vboxsync * This code is based on
4cedb1a5cf9082f72e6b8373e0dc8a15e312ea46vboxsync * with the following copyright and permission notice:
4cedb1a5cf9082f72e6b8373e0dc8a15e312ea46vboxsync * Copyright 2012 Red Hat Inc.
4cedb1a5cf9082f72e6b8373e0dc8a15e312ea46vboxsync * Permission is hereby granted, free of charge, to any person obtaining a
4cedb1a5cf9082f72e6b8373e0dc8a15e312ea46vboxsync * copy of this software and associated documentation files (the
4cedb1a5cf9082f72e6b8373e0dc8a15e312ea46vboxsync * "Software"), to deal in the Software without restriction, including
4cedb1a5cf9082f72e6b8373e0dc8a15e312ea46vboxsync * without limitation the rights to use, copy, modify, merge, publish,
4cedb1a5cf9082f72e6b8373e0dc8a15e312ea46vboxsync * distribute, sub license, and/or sell copies of the Software, and to
4cedb1a5cf9082f72e6b8373e0dc8a15e312ea46vboxsync * permit persons to whom the Software is furnished to do so, subject to
4cedb1a5cf9082f72e6b8373e0dc8a15e312ea46vboxsync * the following conditions:
4cedb1a5cf9082f72e6b8373e0dc8a15e312ea46vboxsync * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
4cedb1a5cf9082f72e6b8373e0dc8a15e312ea46vboxsync * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
4cedb1a5cf9082f72e6b8373e0dc8a15e312ea46vboxsync * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
4cedb1a5cf9082f72e6b8373e0dc8a15e312ea46vboxsync * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
4cedb1a5cf9082f72e6b8373e0dc8a15e312ea46vboxsync * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
4cedb1a5cf9082f72e6b8373e0dc8a15e312ea46vboxsync * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
4cedb1a5cf9082f72e6b8373e0dc8a15e312ea46vboxsync * USE OR OTHER DEALINGS IN THE SOFTWARE.
4cedb1a5cf9082f72e6b8373e0dc8a15e312ea46vboxsync * The above copyright notice and this permission notice (including the
4cedb1a5cf9082f72e6b8373e0dc8a15e312ea46vboxsync * next paragraph) shall be included in all copies or substantial portions
4cedb1a5cf9082f72e6b8373e0dc8a15e312ea46vboxsync * of the Software.
4cedb1a5cf9082f72e6b8373e0dc8a15e312ea46vboxsync * Authors: Dave Airlie <airlied@redhat.com>
4cedb1a5cf9082f72e6b8373e0dc8a15e312ea46vboxsyncstatic void vbox_user_framebuffer_destroy(struct drm_framebuffer *fb)
4cedb1a5cf9082f72e6b8373e0dc8a15e312ea46vboxsync struct vbox_framebuffer *vbox_fb = to_vbox_framebuffer(fb);
4cedb1a5cf9082f72e6b8373e0dc8a15e312ea46vboxsync LogFunc(("vboxvideo: %d: vbox_fb=%p, vbox_fb->obj=%p\n", __LINE__,
4cedb1a5cf9082f72e6b8373e0dc8a15e312ea46vboxsyncstatic int vbox_user_framebuffer_create_handle(struct drm_framebuffer *fb,
4cedb1a5cf9082f72e6b8373e0dc8a15e312ea46vboxsync unsigned int *handle)
4cedb1a5cf9082f72e6b8373e0dc8a15e312ea46vboxsync/** Send information about dirty rectangles to VBVA. If necessary we enable
4cedb1a5cf9082f72e6b8373e0dc8a15e312ea46vboxsync * VBVA first, as this is normally disabled after a mode set in case a user
4cedb1a5cf9082f72e6b8373e0dc8a15e312ea46vboxsync * takes over the console that is not aware of VBVA (i.e. the VESA BIOS). */
4cedb1a5cf9082f72e6b8373e0dc8a15e312ea46vboxsyncvoid vbox_framebuffer_dirty_rectangles(struct drm_framebuffer *fb,
4cedb1a5cf9082f72e6b8373e0dc8a15e312ea46vboxsync unsigned i;
4cedb1a5cf9082f72e6b8373e0dc8a15e312ea46vboxsync LogFunc(("vboxvideo: %d: fb=%p, cRects=%u, vbox=%p\n", __LINE__, fb,
4cedb1a5cf9082f72e6b8373e0dc8a15e312ea46vboxsync for (i = 0; i < cRects; ++i)
4cedb1a5cf9082f72e6b8373e0dc8a15e312ea46vboxsync list_for_each_entry(crtc, &fb->dev->mode_config.crtc_list, head)
4cedb1a5cf9082f72e6b8373e0dc8a15e312ea46vboxsync struct VBVABUFFER *pVBVA = vbox->paVBVACtx[iCrtc].pVBVA;
4cedb1a5cf9082f72e6b8373e0dc8a15e312ea46vboxsync pVBVA = (struct VBVABUFFER *) ( ((uint8_t *)vbox->vram)
4cedb1a5cf9082f72e6b8373e0dc8a15e312ea46vboxsync if (!VBoxVBVAEnable(&vbox->paVBVACtx[iCrtc], &vbox->Ctx, pVBVA, iCrtc))
4cedb1a5cf9082f72e6b8373e0dc8a15e312ea46vboxsync AssertReleaseMsgFailed(("VBoxVBVAEnable failed - heap allocation error, very old host or driver error.\n"));
4cedb1a5cf9082f72e6b8373e0dc8a15e312ea46vboxsync if (VBoxVBVABufferBeginUpdate(&vbox->paVBVACtx[iCrtc],
4cedb1a5cf9082f72e6b8373e0dc8a15e312ea46vboxsync VBoxVBVAWrite(&vbox->paVBVACtx[iCrtc], &vbox->Ctx, &cmdHdr,
4cedb1a5cf9082f72e6b8373e0dc8a15e312ea46vboxsyncstatic int vbox_user_framebuffer_dirty(struct drm_framebuffer *fb,
4cedb1a5cf9082f72e6b8373e0dc8a15e312ea46vboxsync LogFunc(("vboxvideo: %d, flags=%u\n", __LINE__, flags));
4cedb1a5cf9082f72e6b8373e0dc8a15e312ea46vboxsync vbox_framebuffer_dirty_rectangles(fb, pRects, cRects);
4cedb1a5cf9082f72e6b8373e0dc8a15e312ea46vboxsyncstatic const struct drm_framebuffer_funcs vbox_fb_funcs =
4cedb1a5cf9082f72e6b8373e0dc8a15e312ea46vboxsync .create_handle = vbox_user_framebuffer_create_handle,
4cedb1a5cf9082f72e6b8373e0dc8a15e312ea46vboxsync LogFunc(("vboxvideo: %d: dev=%p, vbox_fb=%p, obj=%p\n", __LINE__, dev,
4cedb1a5cf9082f72e6b8373e0dc8a15e312ea46vboxsync drm_helper_mode_fill_fb_struct(&vbox_fb->base, mode_cmd);
4cedb1a5cf9082f72e6b8373e0dc8a15e312ea46vboxsync ret = drm_framebuffer_init(dev, &vbox_fb->base, &vbox_fb_funcs);
4cedb1a5cf9082f72e6b8373e0dc8a15e312ea46vboxsyncstatic struct drm_framebuffer *
4cedb1a5cf9082f72e6b8373e0dc8a15e312ea46vboxsyncvbox_user_framebuffer_create(struct drm_device *dev,
4cedb1a5cf9082f72e6b8373e0dc8a15e312ea46vboxsync obj = drm_gem_object_lookup(dev, filp, mode_cmd->handles[0]);
4cedb1a5cf9082f72e6b8373e0dc8a15e312ea46vboxsync ret = vbox_framebuffer_init(dev, vbox_fb, mode_cmd, obj);
4cedb1a5cf9082f72e6b8373e0dc8a15e312ea46vboxsyncstatic const struct drm_mode_config_funcs vbox_mode_funcs =
4cedb1a5cf9082f72e6b8373e0dc8a15e312ea46vboxsync unsigned i;
4cedb1a5cf9082f72e6b8373e0dc8a15e312ea46vboxsync VBoxVBVADisable(&pVBox->paVBVACtx[i], &pVBox->Ctx, i);
4cedb1a5cf9082f72e6b8373e0dc8a15e312ea46vboxsyncstatic int vbox_vbva_init(struct vbox_private *vbox)
4cedb1a5cf9082f72e6b8373e0dc8a15e312ea46vboxsync unsigned i;
4cedb1a5cf9082f72e6b8373e0dc8a15e312ea46vboxsync LogFunc(("vboxvideo: %d: vbox=%p, vbox->cCrtcs=%u, vbox->paVBVACtx=%p\n",
4cedb1a5cf9082f72e6b8373e0dc8a15e312ea46vboxsync __LINE__, vbox, (unsigned)vbox->cCrtcs, vbox->paVBVACtx));
4cedb1a5cf9082f72e6b8373e0dc8a15e312ea46vboxsync vbox->paVBVACtx = kzalloc( sizeof(struct VBVABUFFERCONTEXT)
4cedb1a5cf9082f72e6b8373e0dc8a15e312ea46vboxsync /* Take a command buffer for each screen from the end of usable VRAM. */
4cedb1a5cf9082f72e6b8373e0dc8a15e312ea46vboxsync vbox->vram_size -= vbox->cCrtcs * VBVA_MIN_BUFFER_SIZE;
4cedb1a5cf9082f72e6b8373e0dc8a15e312ea46vboxsync LogFunc(("vboxvideo: %d: vbox->paVBVACtx=%p, vbox->vram_size=%u\n",
4cedb1a5cf9082f72e6b8373e0dc8a15e312ea46vboxsync __LINE__, vbox->paVBVACtx, (unsigned)vbox->vram_size));
4cedb1a5cf9082f72e6b8373e0dc8a15e312ea46vboxsync/** Allocation function for the HGSMI heap and data. */
4cedb1a5cf9082f72e6b8373e0dc8a15e312ea46vboxsyncstatic DECLCALLBACK(void *) hgsmiEnvAlloc(void *pvEnv, HGSMISIZE cb)
4cedb1a5cf9082f72e6b8373e0dc8a15e312ea46vboxsync/** Free function for the HGSMI heap and data. */
4cedb1a5cf9082f72e6b8373e0dc8a15e312ea46vboxsyncstatic DECLCALLBACK(void) hgsmiEnvFree(void *pvEnv, void *pv)
4cedb1a5cf9082f72e6b8373e0dc8a15e312ea46vboxsync/** Pointers to the HGSMI heap and data manipulation functions. */
4cedb1a5cf9082f72e6b8373e0dc8a15e312ea46vboxsync/** Set up our heaps and data exchange buffers in VRAM before handing the rest
4cedb1a5cf9082f72e6b8373e0dc8a15e312ea46vboxsync * to the memory manager. */
4cedb1a5cf9082f72e6b8373e0dc8a15e312ea46vboxsyncstatic int setupAcceleration(struct vbox_private *pVBox, uint32_t *poffBase)
4cedb1a5cf9082f72e6b8373e0dc8a15e312ea46vboxsync VBoxHGSMIGetBaseMappingInfo(pVBox->full_vram_size, &offBase, NULL,
4cedb1a5cf9082f72e6b8373e0dc8a15e312ea46vboxsync pvGuestHeap = ((uint8_t *)pVBox->vram) + offBase + offGuestHeap;
4cedb1a5cf9082f72e6b8373e0dc8a15e312ea46vboxsync if (RT_FAILURE(VBoxHGSMISetupGuestContext(&pVBox->Ctx, pvGuestHeap,
4cedb1a5cf9082f72e6b8373e0dc8a15e312ea46vboxsync /* Reduce available VRAM size to reflect the guest heap. */
4cedb1a5cf9082f72e6b8373e0dc8a15e312ea46vboxsync /* Linux drm represents monitors as a 32-bit array. */
4cedb1a5cf9082f72e6b8373e0dc8a15e312ea46vboxsync pVBox->cCrtcs = RT_MIN(VBoxHGSMIGetMonitorCount(&pVBox->Ctx), 32);
4cedb1a5cf9082f72e6b8373e0dc8a15e312ea46vboxsyncint vbox_driver_load(struct drm_device *dev, unsigned long flags)
4cedb1a5cf9082f72e6b8373e0dc8a15e312ea46vboxsync LogFunc(("vboxvideo: %d: dev=%p\n", __LINE__, dev));
4cedb1a5cf9082f72e6b8373e0dc8a15e312ea46vboxsync vbox = kzalloc(sizeof(struct vbox_private), GFP_KERNEL);
4cedb1a5cf9082f72e6b8373e0dc8a15e312ea46vboxsync /* I hope this won't interfere with the memory manager. */
4cedb1a5cf9082f72e6b8373e0dc8a15e312ea46vboxsync LogFunc(("vboxvideo: %d: vbox=%p, vbox->vram=%p, vbox->full_vram_size=%u\n",
4cedb1a5cf9082f72e6b8373e0dc8a15e312ea46vboxsync __LINE__, vbox, vbox->vram, (unsigned)vbox->full_vram_size));
4cedb1a5cf9082f72e6b8373e0dc8a15e312ea46vboxsync LogFunc(("vboxvideo: %d: ret=%d\n", __LINE__, ret));
4cedb1a5cf9082f72e6b8373e0dc8a15e312ea46vboxsyncstatic bool drm_fb_helper_restore_fbdev_mode_unlocked(struct drm_fb_helper
4cedb1a5cf9082f72e6b8373e0dc8a15e312ea46vboxsync drm_fb_helper_restore_fbdev_mode_unlocked(&pVBox->fbdev->helper);
4cedb1a5cf9082f72e6b8373e0dc8a15e312ea46vboxsync LogFunc(("vboxvideo: %d: dev=%p, size=%u, iskernel=%u\n", __LINE__,
4cedb1a5cf9082f72e6b8373e0dc8a15e312ea46vboxsync LogFunc(("vboxvideo: %d: obj=%p\n", __LINE__, obj));
4cedb1a5cf9082f72e6b8373e0dc8a15e312ea46vboxsync LogFunc(("vboxvideo: %d: args->width=%u, args->height=%u, args->bpp=%u\n",
4cedb1a5cf9082f72e6b8373e0dc8a15e312ea46vboxsync __LINE__, (unsigned)args->width, (unsigned)args->height,
4cedb1a5cf9082f72e6b8373e0dc8a15e312ea46vboxsync LogFunc(("vboxvideo: %d: args->handle=%u\n", __LINE__,
4cedb1a5cf9082f72e6b8373e0dc8a15e312ea46vboxsync LogFunc(("vboxvideo: %d: dev=%p, handle=%u\n", __LINE__, dev,
4cedb1a5cf9082f72e6b8373e0dc8a15e312ea46vboxsync (unsigned)handle));
4cedb1a5cf9082f72e6b8373e0dc8a15e312ea46vboxsyncvoid vbox_gem_free_object(struct drm_gem_object *obj)
4cedb1a5cf9082f72e6b8373e0dc8a15e312ea46vboxsync LogFunc(("vboxvideo: %d: vbox_bo=%p\n", __LINE__, vbox_bo));
4cedb1a5cf9082f72e6b8373e0dc8a15e312ea46vboxsyncstatic inline u64 vbox_bo_mmap_offset(struct vbox_bo *bo)
4cedb1a5cf9082f72e6b8373e0dc8a15e312ea46vboxsync LogFunc(("vboxvideo: %d: dev=%p, handle=%u\n", __LINE__,
4cedb1a5cf9082f72e6b8373e0dc8a15e312ea46vboxsync LogFunc(("vboxvideo: %d: bo=%p, offset=%llu\n", __LINE__,