1e07dc26bSmrg/*
2e07dc26bSmrg * Copyright (C) 2006-2017 Oracle Corporation
3e07dc26bSmrg *
4e07dc26bSmrg * Permission is hereby granted, free of charge, to any person obtaining a
5e07dc26bSmrg * copy of this software and associated documentation files (the "Software"),
6e07dc26bSmrg * to deal in the Software without restriction, including without limitation
7e07dc26bSmrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8e07dc26bSmrg * and/or sell copies of the Software, and to permit persons to whom the
9e07dc26bSmrg * Software is furnished to do so, subject to the following conditions:
10e07dc26bSmrg *
11e07dc26bSmrg * The above copyright notice and this permission notice shall be included in
12e07dc26bSmrg * all copies or substantial portions of the Software.
13e07dc26bSmrg *
14e07dc26bSmrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15e07dc26bSmrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16e07dc26bSmrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17e07dc26bSmrg * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18e07dc26bSmrg * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19e07dc26bSmrg * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20e07dc26bSmrg * OTHER DEALINGS IN THE SOFTWARE.
21e07dc26bSmrg */
22e07dc26bSmrg
23e07dc26bSmrg#include <VBoxVideoGuest.h>
24e07dc26bSmrg#include <VBoxVideoVBE.h>
25e07dc26bSmrg#include <VBoxVideoIPRT.h>
26e07dc26bSmrg
27e07dc26bSmrg/**
28e07dc26bSmrg * Set up the HGSMI guest-to-host command context.
29e07dc26bSmrg * @returns iprt status value
30e07dc26bSmrg * @param  pCtx                    the context to set up
31e07dc26bSmrg * @param  pvGuestHeapMemory       a pointer to the mapped backing memory for
32e07dc26bSmrg *                                 the guest heap
33e07dc26bSmrg * @param  cbGuestHeapMemory       the size of the backing memory area
34e07dc26bSmrg * @param  offVRAMGuestHeapMemory  the offset of the memory pointed to by
35e07dc26bSmrg *                                 @a pvGuestHeapMemory within the video RAM
36e07dc26bSmrg * @param  pEnv                    HGSMI environment.
37e07dc26bSmrg */
38e07dc26bSmrgDECLHIDDEN(int) VBoxHGSMISetupGuestContext(PHGSMIGUESTCOMMANDCONTEXT pCtx,
39e07dc26bSmrg                                           void *pvGuestHeapMemory,
40e07dc26bSmrg                                           uint32_t cbGuestHeapMemory,
41e07dc26bSmrg                                           uint32_t offVRAMGuestHeapMemory,
42e07dc26bSmrg                                           const HGSMIENV *pEnv)
43e07dc26bSmrg{
44e07dc26bSmrg    /** @todo should we be using a fixed ISA port value here? */
45e07dc26bSmrg    pCtx->port = (RTIOPORT)VGA_PORT_HGSMI_GUEST;
46e07dc26bSmrg#ifdef VBOX_WDDM_MINIPORT
47e07dc26bSmrg    return VBoxSHGSMIInit(&pCtx->heapCtx, pvGuestHeapMemory,
48e07dc26bSmrg                          cbGuestHeapMemory, offVRAMGuestHeapMemory, pEnv);
49e07dc26bSmrg#else
50e07dc26bSmrg    return HGSMIHeapSetup(&pCtx->heapCtx, pvGuestHeapMemory,
51e07dc26bSmrg                          cbGuestHeapMemory, offVRAMGuestHeapMemory, pEnv);
52e07dc26bSmrg#endif
53e07dc26bSmrg}
54e07dc26bSmrg
55e07dc26bSmrg
56e07dc26bSmrg/**
57e07dc26bSmrg * Allocate and initialise a command descriptor in the guest heap for a
58e07dc26bSmrg * guest-to-host command.
59e07dc26bSmrg *
60e07dc26bSmrg * @returns  pointer to the descriptor's command data buffer
61e07dc26bSmrg * @param  pCtx     the context containing the heap to be used
62e07dc26bSmrg * @param  cbData   the size of the command data to go into the descriptor
63e07dc26bSmrg * @param  u8Ch     the HGSMI channel to be used, set to the descriptor
64e07dc26bSmrg * @param  u16Op    the HGSMI command to be sent, set to the descriptor
65e07dc26bSmrg */
66e07dc26bSmrgDECLHIDDEN(void *) VBoxHGSMIBufferAlloc(PHGSMIGUESTCOMMANDCONTEXT pCtx,
67e07dc26bSmrg                                        HGSMISIZE cbData,
68e07dc26bSmrg                                        uint8_t u8Ch,
69e07dc26bSmrg                                        uint16_t u16Op)
70e07dc26bSmrg{
71e07dc26bSmrg#ifdef VBOX_WDDM_MINIPORT
72e07dc26bSmrg    return VBoxSHGSMIHeapAlloc (&pCtx->heapCtx, cbData, u8Ch, u16Op);
73e07dc26bSmrg#else
74e07dc26bSmrg    return HGSMIHeapAlloc (&pCtx->heapCtx, cbData, u8Ch, u16Op);
75e07dc26bSmrg#endif
76e07dc26bSmrg}
77e07dc26bSmrg
78e07dc26bSmrg
79e07dc26bSmrg/**
80e07dc26bSmrg * Free a descriptor allocated by @a VBoxHGSMIBufferAlloc.
81e07dc26bSmrg *
82e07dc26bSmrg * @param  pCtx      the context containing the heap used
83e07dc26bSmrg * @param  pvBuffer  the pointer returned by @a VBoxHGSMIBufferAlloc
84e07dc26bSmrg */
85e07dc26bSmrgDECLHIDDEN(void) VBoxHGSMIBufferFree(PHGSMIGUESTCOMMANDCONTEXT pCtx,
86e07dc26bSmrg                                     void *pvBuffer)
87e07dc26bSmrg{
88e07dc26bSmrg#ifdef VBOX_WDDM_MINIPORT
89e07dc26bSmrg    VBoxSHGSMIHeapFree (&pCtx->heapCtx, pvBuffer);
90e07dc26bSmrg#else
91e07dc26bSmrg    HGSMIHeapFree (&pCtx->heapCtx, pvBuffer);
92e07dc26bSmrg#endif
93e07dc26bSmrg}
94e07dc26bSmrg
95e07dc26bSmrg/**
96e07dc26bSmrg * Submit a command descriptor allocated by @a VBoxHGSMIBufferAlloc.
97e07dc26bSmrg *
98e07dc26bSmrg * @param  pCtx      the context containing the heap used
99e07dc26bSmrg * @param  pvBuffer  the pointer returned by @a VBoxHGSMIBufferAlloc
100e07dc26bSmrg */
101e07dc26bSmrgDECLHIDDEN(int) VBoxHGSMIBufferSubmit(PHGSMIGUESTCOMMANDCONTEXT pCtx,
102e07dc26bSmrg                                      void *pvBuffer)
103e07dc26bSmrg{
104e07dc26bSmrg    /* Initialize the buffer and get the offset for port IO. */
105e07dc26bSmrg    HGSMIOFFSET offBuffer = HGSMIHeapBufferOffset (HGSMIGUESTCMDHEAP_GET(&pCtx->heapCtx), pvBuffer);
106e07dc26bSmrg
107e07dc26bSmrg    Assert(offBuffer != HGSMIOFFSET_VOID);
108e07dc26bSmrg    if (offBuffer != HGSMIOFFSET_VOID)
109e07dc26bSmrg    {
110e07dc26bSmrg        /* Submit the buffer to the host. */
111e07dc26bSmrg        VBVO_PORT_WRITE_U32(pCtx->port, offBuffer);
112e07dc26bSmrg        /* Make the compiler aware that the host has changed memory. */
113e07dc26bSmrg        ASMCompilerBarrier();
114e07dc26bSmrg        return VINF_SUCCESS;
115e07dc26bSmrg    }
116e07dc26bSmrg
117e07dc26bSmrg    return VERR_INVALID_PARAMETER;
118e07dc26bSmrg}
119