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